1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <sys/types.h>
14 #include <sys/epoll.h>
15 #include <sys/queue.h>
17 #include <sys/socket.h>
18 #include <sys/select.h>
22 #pragma message "Jansson dev libs unavailable, not including JSON parsing"
24 #include <rte_string_fns.h>
26 #include <rte_memory.h>
27 #include <rte_malloc.h>
28 #include <rte_cycles.h>
29 #include <rte_ethdev.h>
31 #include <rte_pmd_i40e.h>
33 #include <rte_power.h>
35 #include <libvirt/libvirt.h>
36 #include "channel_monitor.h"
37 #include "channel_manager.h"
38 #include "power_manager.h"
39 #include "oob_monitor.h"
41 #define RTE_LOGTYPE_CHANNEL_MONITOR RTE_LOGTYPE_USER1
43 #define MAX_EVENTS 256
45 uint64_t vsi_pkt_count_prev[384];
46 uint64_t rdtsc_prev[384];
47 #define MAX_JSON_STRING_LEN 1024
48 char json_data[MAX_JSON_STRING_LEN];
50 double time_period_ms = 1;
51 static volatile unsigned run_loop = 1;
52 static int global_event_fd;
53 static unsigned int policy_is_set;
54 static struct epoll_event *global_events_list;
55 static struct policy policies[RTE_MAX_LCORE];
60 struct rte_ether_addr addr;
65 str_to_ether_addr(const char *a, struct rte_ether_addr *ether_addr)
69 unsigned long o[RTE_ETHER_ADDR_LEN];
74 o[i] = strtoul(a, &end, 16);
75 if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
78 } while (++i != RTE_DIM(o) / sizeof(o[0]) && end[0] != 0);
80 /* Junk at the end of line */
84 /* Support the format XX:XX:XX:XX:XX:XX */
85 if (i == RTE_ETHER_ADDR_LEN) {
89 ether_addr->addr_bytes[i] = (uint8_t)o[i];
91 /* Support the format XXXX:XXXX:XXXX */
92 } else if (i == RTE_ETHER_ADDR_LEN / 2) {
94 if (o[i] > UINT16_MAX)
96 ether_addr->addr_bytes[i * 2] =
98 ether_addr->addr_bytes[i * 2 + 1] =
99 (uint8_t)(o[i] & 0xff);
109 set_policy_mac(struct rte_power_channel_packet *pkt, int idx, char *mac)
114 /* Use port MAC address as the vfid */
115 ret = str_to_ether_addr(mac, &pfid.addr);
118 RTE_LOG(ERR, CHANNEL_MONITOR,
119 "Invalid mac address received in JSON\n");
124 printf("Received MAC Address: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":"
125 "%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
126 RTE_ETHER_ADDR_BYTES(&pfid.addr));
128 pkt->vfid[idx] = pfid.pfid;
133 get_resource_name_from_chn_path(const char *channel_path)
137 substr = strstr(channel_path, CHANNEL_MGR_FIFO_PATTERN_NAME);
143 get_resource_id_from_vmname(const char *vm_name)
151 while (vm_name[off] != '\0') {
152 if (isdigit(vm_name[off]))
156 result = atoi(&vm_name[off]);
157 if ((result == 0) && (vm_name[off] != '0'))
164 parse_json_to_pkt(json_t *element, struct rte_power_channel_packet *pkt,
172 memset(pkt, 0, sizeof(*pkt));
174 pkt->nb_mac_to_monitor = 0;
175 pkt->t_boost_status.tbEnabled = false;
176 pkt->workload = RTE_POWER_WL_LOW;
177 pkt->policy_to_use = RTE_POWER_POLICY_TIME;
178 pkt->command = RTE_POWER_PKT_POLICY;
179 pkt->core_type = RTE_POWER_CORE_TYPE_PHYSICAL;
181 if (vm_name == NULL) {
182 RTE_LOG(ERR, CHANNEL_MONITOR,
183 "vm_name is NULL, request rejected !\n");
187 json_object_foreach(element, key, value) {
188 if (!strcmp(key, "policy")) {
189 /* Recurse in to get the contents of profile */
190 ret = parse_json_to_pkt(value, pkt, vm_name);
193 } else if (!strcmp(key, "instruction")) {
194 /* Recurse in to get the contents of instruction */
195 ret = parse_json_to_pkt(value, pkt, vm_name);
198 } else if (!strcmp(key, "command")) {
200 strlcpy(command, json_string_value(value), 32);
201 if (!strcmp(command, "power")) {
202 pkt->command = RTE_POWER_CPU_POWER;
203 } else if (!strcmp(command, "create")) {
204 pkt->command = RTE_POWER_PKT_POLICY;
205 } else if (!strcmp(command, "destroy")) {
206 pkt->command = RTE_POWER_PKT_POLICY_REMOVE;
208 RTE_LOG(ERR, CHANNEL_MONITOR,
209 "Invalid command received in JSON\n");
212 } else if (!strcmp(key, "policy_type")) {
214 strlcpy(command, json_string_value(value), 32);
215 if (!strcmp(command, "TIME")) {
217 RTE_POWER_POLICY_TIME;
218 } else if (!strcmp(command, "TRAFFIC")) {
220 RTE_POWER_POLICY_TRAFFIC;
221 } else if (!strcmp(command, "WORKLOAD")) {
223 RTE_POWER_POLICY_WORKLOAD;
224 } else if (!strcmp(command, "BRANCH_RATIO")) {
226 RTE_POWER_POLICY_BRANCH_RATIO;
228 RTE_LOG(ERR, CHANNEL_MONITOR,
229 "Wrong policy_type received in JSON\n");
232 } else if (!strcmp(key, "workload")) {
234 strlcpy(command, json_string_value(value), 32);
235 if (!strcmp(command, "HIGH")) {
236 pkt->workload = RTE_POWER_WL_HIGH;
237 } else if (!strcmp(command, "MEDIUM")) {
238 pkt->workload = RTE_POWER_WL_MEDIUM;
239 } else if (!strcmp(command, "LOW")) {
240 pkt->workload = RTE_POWER_WL_LOW;
242 RTE_LOG(ERR, CHANNEL_MONITOR,
243 "Wrong workload received in JSON\n");
246 } else if (!strcmp(key, "busy_hours")) {
248 size_t size = json_array_size(value);
250 for (i = 0; i < size; i++) {
251 int hour = (int)json_integer_value(
252 json_array_get(value, i));
253 pkt->timer_policy.busy_hours[i] = hour;
255 } else if (!strcmp(key, "quiet_hours")) {
257 size_t size = json_array_size(value);
259 for (i = 0; i < size; i++) {
260 int hour = (int)json_integer_value(
261 json_array_get(value, i));
262 pkt->timer_policy.quiet_hours[i] = hour;
264 } else if (!strcmp(key, "mac_list")) {
266 size_t size = json_array_size(value);
268 for (i = 0; i < size; i++) {
271 json_string_value(json_array_get(value, i)),
273 set_policy_mac(pkt, i, mac);
275 pkt->nb_mac_to_monitor = size;
276 } else if (!strcmp(key, "avg_packet_thresh")) {
277 pkt->traffic_policy.avg_max_packet_thresh =
278 (uint32_t)json_integer_value(value);
279 } else if (!strcmp(key, "max_packet_thresh")) {
280 pkt->traffic_policy.max_max_packet_thresh =
281 (uint32_t)json_integer_value(value);
282 } else if (!strcmp(key, "unit")) {
284 strlcpy(unit, json_string_value(value), 32);
285 if (!strcmp(unit, "SCALE_UP")) {
286 pkt->unit = RTE_POWER_SCALE_UP;
287 } else if (!strcmp(unit, "SCALE_DOWN")) {
288 pkt->unit = RTE_POWER_SCALE_DOWN;
289 } else if (!strcmp(unit, "SCALE_MAX")) {
290 pkt->unit = RTE_POWER_SCALE_MAX;
291 } else if (!strcmp(unit, "SCALE_MIN")) {
292 pkt->unit = RTE_POWER_SCALE_MIN;
293 } else if (!strcmp(unit, "ENABLE_TURBO")) {
294 pkt->unit = RTE_POWER_ENABLE_TURBO;
295 } else if (!strcmp(unit, "DISABLE_TURBO")) {
296 pkt->unit = RTE_POWER_DISABLE_TURBO;
298 RTE_LOG(ERR, CHANNEL_MONITOR,
299 "Invalid command received in JSON\n");
303 RTE_LOG(ERR, CHANNEL_MONITOR,
304 "Unknown key received in JSON string: %s\n",
308 resource_id = get_resource_id_from_vmname(vm_name);
309 if (resource_id < 0) {
310 RTE_LOG(ERR, CHANNEL_MONITOR,
311 "Could not get resource_id from vm_name:%s\n",
315 strlcpy(pkt->vm_name, vm_name, RTE_POWER_VM_MAX_NAME_SZ);
316 pkt->resource_id = resource_id;
322 void channel_monitor_exit(void)
325 rte_free(global_events_list);
329 core_share(int pNo, int z, int x, int t)
331 if (policies[pNo].core_share[z].pcpu == lvm_info[x].pcpus[t]) {
332 if (strcmp(policies[pNo].pkt.vm_name,
333 lvm_info[x].vm_name) != 0) {
334 policies[pNo].core_share[z].status = 1;
335 power_manager_scale_core_max(
336 policies[pNo].core_share[z].pcpu);
342 core_share_status(int pNo)
345 int noVms = 0, noVcpus = 0, z, x, t;
347 get_all_vm(&noVms, &noVcpus);
349 /* Reset Core Share Status. */
350 for (z = 0; z < noVcpus; z++)
351 policies[pNo].core_share[z].status = 0;
353 /* Foreach vcpu in a policy. */
354 for (z = 0; z < policies[pNo].pkt.num_vcpu; z++) {
355 /* Foreach VM on the platform. */
356 for (x = 0; x < noVms; x++) {
357 /* Foreach vcpu of VMs on platform. */
358 for (t = 0; t < lvm_info[x].num_cpus; t++)
359 core_share(pNo, z, x, t);
366 pcpu_monitor(struct policy *pol, struct core_info *ci, int pcpu, int count)
370 if (pol->pkt.policy_to_use == RTE_POWER_POLICY_BRANCH_RATIO) {
371 ci->cd[pcpu].oob_enabled = 1;
372 ret = add_core_to_monitor(pcpu);
374 RTE_LOG(INFO, CHANNEL_MONITOR,
375 "Monitoring pcpu %d OOB for %s\n",
376 pcpu, pol->pkt.vm_name);
378 RTE_LOG(ERR, CHANNEL_MONITOR,
379 "Error monitoring pcpu %d OOB for %s\n",
380 pcpu, pol->pkt.vm_name);
383 pol->core_share[count].pcpu = pcpu;
384 RTE_LOG(INFO, CHANNEL_MONITOR,
385 "Monitoring pcpu %d for %s\n",
386 pcpu, pol->pkt.vm_name);
392 get_pcpu_to_control(struct policy *pol)
395 /* Convert vcpu to pcpu. */
398 struct core_info *ci;
400 ci = get_core_info();
402 RTE_LOG(DEBUG, CHANNEL_MONITOR,
403 "Looking for pcpu for %s\n", pol->pkt.vm_name);
406 * So now that we're handling virtual and physical cores, we need to
407 * differenciate between them when adding them to the branch monitor.
408 * Virtual cores need to be converted to physical cores.
410 if (pol->pkt.core_type == RTE_POWER_CORE_TYPE_VIRTUAL) {
412 * If the cores in the policy are virtual, we need to map them
413 * to physical core. We look up the vm info and use that for
416 get_info_vm(pol->pkt.vm_name, &info);
417 for (count = 0; count < pol->pkt.num_vcpu; count++) {
418 pcpu = info.pcpu_map[pol->pkt.vcpu_to_control[count]];
419 pcpu_monitor(pol, ci, pcpu, count);
423 * If the cores in the policy are physical, we just use
424 * those core id's directly.
426 for (count = 0; count < pol->pkt.num_vcpu; count++) {
427 pcpu = pol->pkt.vcpu_to_control[count];
428 pcpu_monitor(pol, ci, pcpu, count);
434 get_pfid(struct policy *pol)
439 for (i = 0; i < pol->pkt.nb_mac_to_monitor; i++) {
441 RTE_ETH_FOREACH_DEV(x) {
443 ret = rte_pmd_i40e_query_vfid_by_mac(x,
444 (struct rte_ether_addr *)&(pol->pkt.vfid[i]));
448 if (ret != -EINVAL) {
453 if (ret == -EINVAL || ret == -ENOTSUP || ret == ENODEV) {
454 RTE_LOG(INFO, CHANNEL_MONITOR,
455 "Error with Policy. MAC not found on "
466 update_policy(struct rte_power_channel_packet *pkt)
469 unsigned int updated = 0;
473 RTE_LOG(INFO, CHANNEL_MONITOR,
474 "Applying policy for %s\n", pkt->vm_name);
476 for (i = 0; i < RTE_DIM(policies); i++) {
477 if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
478 /* Copy the contents of *pkt into the policy.pkt */
479 policies[i].pkt = *pkt;
480 get_pcpu_to_control(&policies[i]);
481 /* Check Eth dev only for Traffic policy */
482 if (policies[i].pkt.policy_to_use ==
483 RTE_POWER_POLICY_TRAFFIC) {
484 if (get_pfid(&policies[i]) < 0) {
489 core_share_status(i);
490 policies[i].enabled = 1;
495 for (i = 0; i < RTE_DIM(policies); i++) {
496 if (policies[i].enabled == 0) {
497 policies[i].pkt = *pkt;
498 get_pcpu_to_control(&policies[i]);
499 /* Check Eth dev only for Traffic policy */
500 if (policies[i].pkt.policy_to_use ==
501 RTE_POWER_POLICY_TRAFFIC) {
502 if (get_pfid(&policies[i]) < 0) {
507 core_share_status(i);
508 policies[i].enabled = 1;
517 remove_policy(struct rte_power_channel_packet *pkt __rte_unused)
522 * Disabling the policy is simply a case of setting
525 for (i = 0; i < RTE_DIM(policies); i++) {
526 if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
527 policies[i].enabled = 0;
535 get_pkt_diff(struct policy *pol)
538 uint64_t vsi_pkt_count,
540 vsi_pkt_count_prev_total = 0;
541 double rdtsc_curr, rdtsc_diff, diff;
544 struct rte_eth_stats vf_stats;
547 for (x = 0; x < pol->pkt.nb_mac_to_monitor; x++) {
551 if (rte_pmd_i40e_get_vf_stats(x, pol->pfid[x], &vf_stats) == 0)
552 vsi_pkt_count = vf_stats.ipackets;
559 vsi_pkt_total += vsi_pkt_count;
561 vsi_pkt_count_prev_total += vsi_pkt_count_prev[pol->pfid[x]];
562 vsi_pkt_count_prev[pol->pfid[x]] = vsi_pkt_count;
565 rdtsc_curr = rte_rdtsc_precise();
566 rdtsc_diff = rdtsc_curr - rdtsc_prev[pol->pfid[x-1]];
567 rdtsc_prev[pol->pfid[x-1]] = rdtsc_curr;
569 diff = (vsi_pkt_total - vsi_pkt_count_prev_total) *
570 ((double)rte_get_tsc_hz() / rdtsc_diff);
576 apply_traffic_profile(struct policy *pol)
582 diff = get_pkt_diff(pol);
584 if (diff >= (pol->pkt.traffic_policy.max_max_packet_thresh)) {
585 for (count = 0; count < pol->pkt.num_vcpu; count++) {
586 if (pol->core_share[count].status != 1)
587 power_manager_scale_core_max(
588 pol->core_share[count].pcpu);
590 } else if (diff >= (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
591 for (count = 0; count < pol->pkt.num_vcpu; count++) {
592 if (pol->core_share[count].status != 1)
593 power_manager_scale_core_med(
594 pol->core_share[count].pcpu);
596 } else if (diff < (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
597 for (count = 0; count < pol->pkt.num_vcpu; count++) {
598 if (pol->core_share[count].status != 1)
599 power_manager_scale_core_min(
600 pol->core_share[count].pcpu);
606 apply_time_profile(struct policy *pol)
612 char time_string[40];
614 /* Obtain the time of day, and convert it to a tm struct. */
615 gettimeofday(&tv, NULL);
616 ptm = localtime(&tv.tv_sec);
617 /* Format the date and time, down to a single second. */
618 strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);
620 for (x = 0; x < RTE_POWER_HOURS_PER_DAY; x++) {
622 if (ptm->tm_hour == pol->pkt.timer_policy.busy_hours[x]) {
623 for (count = 0; count < pol->pkt.num_vcpu; count++) {
624 if (pol->core_share[count].status != 1) {
625 power_manager_scale_core_max(
626 pol->core_share[count].pcpu);
630 } else if (ptm->tm_hour ==
631 pol->pkt.timer_policy.quiet_hours[x]) {
632 for (count = 0; count < pol->pkt.num_vcpu; count++) {
633 if (pol->core_share[count].status != 1) {
634 power_manager_scale_core_min(
635 pol->core_share[count].pcpu);
639 } else if (ptm->tm_hour ==
640 pol->pkt.timer_policy.hours_to_use_traffic_profile[x]) {
641 apply_traffic_profile(pol);
648 apply_workload_profile(struct policy *pol)
653 if (pol->pkt.workload == RTE_POWER_WL_HIGH) {
654 for (count = 0; count < pol->pkt.num_vcpu; count++) {
655 if (pol->core_share[count].status != 1)
656 power_manager_scale_core_max(
657 pol->core_share[count].pcpu);
659 } else if (pol->pkt.workload == RTE_POWER_WL_MEDIUM) {
660 for (count = 0; count < pol->pkt.num_vcpu; count++) {
661 if (pol->core_share[count].status != 1)
662 power_manager_scale_core_med(
663 pol->core_share[count].pcpu);
665 } else if (pol->pkt.workload == RTE_POWER_WL_LOW) {
666 for (count = 0; count < pol->pkt.num_vcpu; count++) {
667 if (pol->core_share[count].status != 1)
668 power_manager_scale_core_min(
669 pol->core_share[count].pcpu);
675 apply_policy(struct policy *pol)
678 struct rte_power_channel_packet *pkt = &pol->pkt;
680 /*Check policy to use*/
681 if (pkt->policy_to_use == RTE_POWER_POLICY_TRAFFIC)
682 apply_traffic_profile(pol);
683 else if (pkt->policy_to_use == RTE_POWER_POLICY_TIME)
684 apply_time_profile(pol);
685 else if (pkt->policy_to_use == RTE_POWER_POLICY_WORKLOAD)
686 apply_workload_profile(pol);
690 write_binary_packet(void *buffer,
692 struct channel_info *chan_info)
696 if (buffer_len == 0 || buffer == NULL)
699 if (chan_info->fd < 0) {
700 RTE_LOG(ERR, CHANNEL_MONITOR, "Channel is not connected\n");
704 while (buffer_len > 0) {
705 ret = write(chan_info->fd, buffer, buffer_len);
709 RTE_LOG(ERR, CHANNEL_MONITOR, "Write function failed due to %s.\n",
713 buffer = (char *)buffer + ret;
720 send_freq(struct rte_power_channel_packet *pkt,
721 struct channel_info *chan_info,
724 unsigned int vcore_id = pkt->resource_id;
725 struct rte_power_channel_packet_freq_list channel_pkt_freq_list;
728 if (get_info_vm(pkt->vm_name, &info) != 0)
731 if (!freq_list && vcore_id >= RTE_POWER_MAX_VCPU_PER_VM)
734 if (!info.allow_query)
737 channel_pkt_freq_list.command = RTE_POWER_FREQ_LIST;
738 channel_pkt_freq_list.num_vcpu = info.num_vcpus;
742 for (i = 0; i < info.num_vcpus; i++)
743 channel_pkt_freq_list.freq_list[i] =
744 power_manager_get_current_frequency(info.pcpu_map[i]);
746 channel_pkt_freq_list.freq_list[vcore_id] =
747 power_manager_get_current_frequency(info.pcpu_map[vcore_id]);
750 return write_binary_packet(&channel_pkt_freq_list,
751 sizeof(channel_pkt_freq_list),
756 send_capabilities(struct rte_power_channel_packet *pkt,
757 struct channel_info *chan_info,
760 unsigned int vcore_id = pkt->resource_id;
761 struct rte_power_channel_packet_caps_list channel_pkt_caps_list;
763 struct rte_power_core_capabilities caps;
766 if (get_info_vm(pkt->vm_name, &info) != 0)
769 if (!list_requested && vcore_id >= RTE_POWER_MAX_VCPU_PER_VM)
772 if (!info.allow_query)
775 channel_pkt_caps_list.command = RTE_POWER_CAPS_LIST;
776 channel_pkt_caps_list.num_vcpu = info.num_vcpus;
778 if (list_requested) {
780 for (i = 0; i < info.num_vcpus; i++) {
781 ret = rte_power_get_capabilities(info.pcpu_map[i],
784 channel_pkt_caps_list.turbo[i] =
786 channel_pkt_caps_list.priority[i] =
793 ret = rte_power_get_capabilities(info.pcpu_map[vcore_id],
796 channel_pkt_caps_list.turbo[vcore_id] =
798 channel_pkt_caps_list.priority[vcore_id] =
804 return write_binary_packet(&channel_pkt_caps_list,
805 sizeof(channel_pkt_caps_list),
810 send_ack_for_received_cmd(struct rte_power_channel_packet *pkt,
811 struct channel_info *chan_info,
814 pkt->command = command;
815 return write_binary_packet(pkt,
821 process_request(struct rte_power_channel_packet *pkt,
822 struct channel_info *chan_info)
826 if (chan_info == NULL)
829 uint32_t channel_connected = CHANNEL_MGR_CHANNEL_CONNECTED;
830 if (__atomic_compare_exchange_n(&(chan_info->status), &channel_connected,
831 CHANNEL_MGR_CHANNEL_PROCESSING, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED) == 0)
834 if (pkt->command == RTE_POWER_CPU_POWER) {
835 unsigned int core_num;
837 if (pkt->core_type == RTE_POWER_CORE_TYPE_VIRTUAL)
838 core_num = get_pcpu(chan_info, pkt->resource_id);
840 core_num = pkt->resource_id;
842 RTE_LOG(DEBUG, CHANNEL_MONITOR, "Processing requested cmd for cpu:%d\n",
846 bool valid_unit = true;
849 case(RTE_POWER_SCALE_MIN):
850 scale_res = power_manager_scale_core_min(core_num);
852 case(RTE_POWER_SCALE_MAX):
853 scale_res = power_manager_scale_core_max(core_num);
855 case(RTE_POWER_SCALE_DOWN):
856 scale_res = power_manager_scale_core_down(core_num);
858 case(RTE_POWER_SCALE_UP):
859 scale_res = power_manager_scale_core_up(core_num);
861 case(RTE_POWER_ENABLE_TURBO):
862 scale_res = power_manager_enable_turbo_core(core_num);
864 case(RTE_POWER_DISABLE_TURBO):
865 scale_res = power_manager_disable_turbo_core(core_num);
873 ret = send_ack_for_received_cmd(pkt,
879 RTE_LOG(ERR, CHANNEL_MONITOR, "Error during sending ack command.\n");
881 RTE_LOG(ERR, CHANNEL_MONITOR, "Unexpected unit type.\n");
885 if (pkt->command == RTE_POWER_PKT_POLICY) {
886 RTE_LOG(INFO, CHANNEL_MONITOR, "Processing policy request %s\n",
888 int ret = send_ack_for_received_cmd(pkt,
892 RTE_LOG(ERR, CHANNEL_MONITOR, "Error during sending ack command.\n");
897 if (pkt->command == RTE_POWER_PKT_POLICY_REMOVE) {
898 ret = remove_policy(pkt);
900 RTE_LOG(INFO, CHANNEL_MONITOR,
901 "Removed policy %s\n", pkt->vm_name);
903 RTE_LOG(INFO, CHANNEL_MONITOR,
904 "Policy %s does not exist\n", pkt->vm_name);
907 if (pkt->command == RTE_POWER_QUERY_FREQ_LIST ||
908 pkt->command == RTE_POWER_QUERY_FREQ) {
910 RTE_LOG(INFO, CHANNEL_MONITOR,
911 "Frequency for %s requested.\n", pkt->vm_name);
912 int ret = send_freq(pkt,
914 pkt->command == RTE_POWER_QUERY_FREQ_LIST);
916 RTE_LOG(ERR, CHANNEL_MONITOR, "Error during frequency sending.\n");
919 if (pkt->command == RTE_POWER_QUERY_CAPS_LIST ||
920 pkt->command == RTE_POWER_QUERY_CAPS) {
922 RTE_LOG(INFO, CHANNEL_MONITOR,
923 "Capabilities for %s requested.\n", pkt->vm_name);
924 int ret = send_capabilities(pkt,
926 pkt->command == RTE_POWER_QUERY_CAPS_LIST);
928 RTE_LOG(ERR, CHANNEL_MONITOR, "Error during sending capabilities.\n");
932 * Return is not checked as channel status may have been set to DISABLED
933 * from management thread
935 uint32_t channel_processing = CHANNEL_MGR_CHANNEL_PROCESSING;
936 __atomic_compare_exchange_n(&(chan_info->status), &channel_processing,
937 CHANNEL_MGR_CHANNEL_CONNECTED, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
943 add_channel_to_monitor(struct channel_info **chan_info)
945 struct channel_info *info = *chan_info;
946 struct epoll_event event;
948 event.events = EPOLLIN;
949 event.data.ptr = info;
950 if (epoll_ctl(global_event_fd, EPOLL_CTL_ADD, info->fd, &event) < 0) {
951 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to add channel '%s' "
952 "to epoll\n", info->channel_path);
955 RTE_LOG(ERR, CHANNEL_MONITOR, "Added channel '%s' "
956 "to monitor\n", info->channel_path);
961 remove_channel_from_monitor(struct channel_info *chan_info)
963 if (epoll_ctl(global_event_fd, EPOLL_CTL_DEL,
964 chan_info->fd, NULL) < 0) {
965 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to remove channel '%s' "
966 "from epoll\n", chan_info->channel_path);
973 channel_monitor_init(void)
975 global_event_fd = epoll_create1(0);
976 if (global_event_fd == 0) {
977 RTE_LOG(ERR, CHANNEL_MONITOR,
978 "Error creating epoll context with error %s\n",
982 global_events_list = rte_malloc("epoll_events",
983 sizeof(*global_events_list)
984 * MAX_EVENTS, RTE_CACHE_LINE_SIZE);
985 if (global_events_list == NULL) {
986 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to rte_malloc for "
994 read_binary_packet(struct channel_info *chan_info)
996 struct rte_power_channel_packet pkt;
998 int buffer_len = sizeof(pkt);
999 int n_bytes, err = 0;
1001 while (buffer_len > 0) {
1002 n_bytes = read(chan_info->fd,
1003 buffer, buffer_len);
1004 if (n_bytes == buffer_len)
1008 RTE_LOG(DEBUG, CHANNEL_MONITOR,
1009 "Received error on "
1010 "channel '%s' read: %s\n",
1011 chan_info->channel_path,
1013 remove_channel(&chan_info);
1016 buffer = (char *)buffer + n_bytes;
1017 buffer_len -= n_bytes;
1020 process_request(&pkt, chan_info);
1025 read_json_packet(struct channel_info *chan_info)
1027 struct rte_power_channel_packet pkt;
1031 const char *resource_name;
1036 /* read opening brace to closing brace */
1041 n_bytes = read(chan_info->fd, &json_data[idx], 1);
1044 if (json_data[idx] == '{')
1046 if (json_data[idx] == '}')
1048 if ((indent > 0) || (idx > 0))
1052 if (idx >= MAX_JSON_STRING_LEN-1)
1054 } while (indent > 0);
1056 json_data[idx] = '\0';
1058 if (strlen(json_data) == 0)
1061 printf("got [%s]\n", json_data);
1063 root = json_loads(json_data, 0, &error);
1066 resource_name = get_resource_name_from_chn_path(
1067 chan_info->channel_path);
1069 * Because our data is now in the json
1070 * object, we can overwrite the pkt
1071 * with a rte_power_channel_packet struct, using
1072 * parse_json_to_pkt()
1074 ret = parse_json_to_pkt(root, &pkt, resource_name);
1077 RTE_LOG(ERR, CHANNEL_MONITOR,
1078 "Error validating JSON profile data\n");
1081 start = strstr(pkt.vm_name,
1082 CHANNEL_MGR_FIFO_PATTERN_NAME);
1083 if (start != NULL) {
1084 /* move past pattern to start of fifo id */
1085 start += strlen(CHANNEL_MGR_FIFO_PATTERN_NAME);
1088 n = (uint32_t)strtoul(start, &end, 10);
1090 if (end[0] == '\0') {
1091 /* Add core id to core list */
1093 pkt.vcpu_to_control[0] = n;
1094 process_request(&pkt, chan_info);
1096 RTE_LOG(ERR, CHANNEL_MONITOR,
1097 "Cannot extract core id from fifo name\n");
1100 process_request(&pkt, chan_info);
1103 RTE_LOG(ERR, CHANNEL_MONITOR,
1104 "JSON error on line %d: %s\n",
1105 error.line, error.text);
1107 } while (n_bytes > 0);
1112 run_channel_monitor(void)
1117 n_events = epoll_wait(global_event_fd, global_events_list,
1121 for (i = 0; i < n_events; i++) {
1122 struct channel_info *chan_info = (struct channel_info *)
1123 global_events_list[i].data.ptr;
1124 if ((global_events_list[i].events & EPOLLERR) ||
1125 (global_events_list[i].events & EPOLLHUP)) {
1126 RTE_LOG(INFO, CHANNEL_MONITOR,
1127 "Remote closed connection for "
1129 chan_info->channel_path);
1130 remove_channel(&chan_info);
1133 if (global_events_list[i].events & EPOLLIN) {
1135 switch (chan_info->type) {
1136 case CHANNEL_TYPE_BINARY:
1137 read_binary_packet(chan_info);
1140 case CHANNEL_TYPE_JSON:
1141 read_json_packet(chan_info);
1149 rte_delay_us(time_period_ms*1000);
1150 if (policy_is_set) {
1153 for (j = 0; j < RTE_DIM(policies); j++) {
1154 if (policies[j].enabled == 1)
1155 apply_policy(&policies[j]);