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_atomic.h>
29 #include <rte_cycles.h>
30 #include <rte_ethdev.h>
32 #include <rte_pmd_i40e.h>
34 #include <rte_power.h>
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"
42 #define RTE_LOGTYPE_CHANNEL_MONITOR RTE_LOGTYPE_USER1
44 #define MAX_EVENTS 256
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];
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];
61 struct rte_ether_addr addr;
66 str_to_ether_addr(const char *a, struct rte_ether_addr *ether_addr)
70 unsigned long o[RTE_ETHER_ADDR_LEN];
75 o[i] = strtoul(a, &end, 16);
76 if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
79 } while (++i != RTE_DIM(o) / sizeof(o[0]) && end[0] != 0);
81 /* Junk at the end of line */
85 /* Support the format XX:XX:XX:XX:XX:XX */
86 if (i == RTE_ETHER_ADDR_LEN) {
90 ether_addr->addr_bytes[i] = (uint8_t)o[i];
92 /* Support the format XXXX:XXXX:XXXX */
93 } else if (i == RTE_ETHER_ADDR_LEN / 2) {
95 if (o[i] > UINT16_MAX)
97 ether_addr->addr_bytes[i * 2] =
99 ether_addr->addr_bytes[i * 2 + 1] =
100 (uint8_t)(o[i] & 0xff);
110 set_policy_mac(struct rte_power_channel_packet *pkt, int idx, char *mac)
115 /* Use port MAC address as the vfid */
116 ret = str_to_ether_addr(mac, &pfid.addr);
119 RTE_LOG(ERR, CHANNEL_MONITOR,
120 "Invalid mac address received in JSON\n");
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));
129 pkt->vfid[idx] = pfid.pfid;
134 get_resource_name_from_chn_path(const char *channel_path)
138 substr = strstr(channel_path, CHANNEL_MGR_FIFO_PATTERN_NAME);
144 get_resource_id_from_vmname(const char *vm_name)
152 while (vm_name[off] != '\0') {
153 if (isdigit(vm_name[off]))
157 result = atoi(&vm_name[off]);
158 if ((result == 0) && (vm_name[off] != '0'))
165 parse_json_to_pkt(json_t *element, struct rte_power_channel_packet *pkt,
173 memset(pkt, 0, sizeof(*pkt));
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;
182 if (vm_name == NULL) {
183 RTE_LOG(ERR, CHANNEL_MONITOR,
184 "vm_name is NULL, request rejected !\n");
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);
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);
199 } else if (!strcmp(key, "command")) {
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;
209 RTE_LOG(ERR, CHANNEL_MONITOR,
210 "Invalid command received in JSON\n");
213 } else if (!strcmp(key, "policy_type")) {
215 strlcpy(command, json_string_value(value), 32);
216 if (!strcmp(command, "TIME")) {
218 RTE_POWER_POLICY_TIME;
219 } else if (!strcmp(command, "TRAFFIC")) {
221 RTE_POWER_POLICY_TRAFFIC;
222 } else if (!strcmp(command, "WORKLOAD")) {
224 RTE_POWER_POLICY_WORKLOAD;
225 } else if (!strcmp(command, "BRANCH_RATIO")) {
227 RTE_POWER_POLICY_BRANCH_RATIO;
229 RTE_LOG(ERR, CHANNEL_MONITOR,
230 "Wrong policy_type received in JSON\n");
233 } else if (!strcmp(key, "workload")) {
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;
243 RTE_LOG(ERR, CHANNEL_MONITOR,
244 "Wrong workload received in JSON\n");
247 } else if (!strcmp(key, "busy_hours")) {
249 size_t size = json_array_size(value);
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;
256 } else if (!strcmp(key, "quiet_hours")) {
258 size_t size = json_array_size(value);
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;
265 } else if (!strcmp(key, "mac_list")) {
267 size_t size = json_array_size(value);
269 for (i = 0; i < size; i++) {
272 json_string_value(json_array_get(value, i)),
274 set_policy_mac(pkt, i, mac);
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")) {
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;
299 RTE_LOG(ERR, CHANNEL_MONITOR,
300 "Invalid command received in JSON\n");
304 RTE_LOG(ERR, CHANNEL_MONITOR,
305 "Unknown key received in JSON string: %s\n",
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",
316 strlcpy(pkt->vm_name, vm_name, RTE_POWER_VM_MAX_NAME_SZ);
317 pkt->resource_id = resource_id;
323 void channel_monitor_exit(void)
326 rte_free(global_events_list);
330 core_share(int pNo, int z, int x, int t)
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);
343 core_share_status(int pNo)
346 int noVms = 0, noVcpus = 0, z, x, t;
348 get_all_vm(&noVms, &noVcpus);
350 /* Reset Core Share Status. */
351 for (z = 0; z < noVcpus; z++)
352 policies[pNo].core_share[z].status = 0;
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);
367 pcpu_monitor(struct policy *pol, struct core_info *ci, int pcpu, int count)
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);
375 RTE_LOG(INFO, CHANNEL_MONITOR,
376 "Monitoring pcpu %d OOB for %s\n",
377 pcpu, pol->pkt.vm_name);
379 RTE_LOG(ERR, CHANNEL_MONITOR,
380 "Error monitoring pcpu %d OOB for %s\n",
381 pcpu, pol->pkt.vm_name);
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);
393 get_pcpu_to_control(struct policy *pol)
396 /* Convert vcpu to pcpu. */
399 struct core_info *ci;
401 ci = get_core_info();
403 RTE_LOG(DEBUG, CHANNEL_MONITOR,
404 "Looking for pcpu for %s\n", pol->pkt.vm_name);
407 * So now that we're handling virtual and physical cores, we need to
408 * differenciate between them when adding them to the branch monitor.
409 * Virtual cores need to be converted to physical cores.
411 if (pol->pkt.core_type == RTE_POWER_CORE_TYPE_VIRTUAL) {
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
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);
424 * If the cores in the policy are physical, we just use
425 * those core id's directly.
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);
435 get_pfid(struct policy *pol)
440 for (i = 0; i < pol->pkt.nb_mac_to_monitor; i++) {
442 RTE_ETH_FOREACH_DEV(x) {
444 ret = rte_pmd_i40e_query_vfid_by_mac(x,
445 (struct rte_ether_addr *)&(pol->pkt.vfid[i]));
449 if (ret != -EINVAL) {
454 if (ret == -EINVAL || ret == -ENOTSUP || ret == ENODEV) {
455 RTE_LOG(INFO, CHANNEL_MONITOR,
456 "Error with Policy. MAC not found on "
467 update_policy(struct rte_power_channel_packet *pkt)
470 unsigned int updated = 0;
474 RTE_LOG(INFO, CHANNEL_MONITOR,
475 "Applying policy for %s\n", pkt->vm_name);
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) {
490 core_share_status(i);
491 policies[i].enabled = 1;
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) {
508 core_share_status(i);
509 policies[i].enabled = 1;
518 remove_policy(struct rte_power_channel_packet *pkt __rte_unused)
523 * Disabling the policy is simply a case of setting
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;
536 get_pkt_diff(struct policy *pol)
539 uint64_t vsi_pkt_count,
541 vsi_pkt_count_prev_total = 0;
542 double rdtsc_curr, rdtsc_diff, diff;
545 struct rte_eth_stats vf_stats;
548 for (x = 0; x < pol->pkt.nb_mac_to_monitor; x++) {
552 if (rte_pmd_i40e_get_vf_stats(x, pol->pfid[x], &vf_stats) == 0)
553 vsi_pkt_count = vf_stats.ipackets;
560 vsi_pkt_total += vsi_pkt_count;
562 vsi_pkt_count_prev_total += vsi_pkt_count_prev[pol->pfid[x]];
563 vsi_pkt_count_prev[pol->pfid[x]] = vsi_pkt_count;
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;
570 diff = (vsi_pkt_total - vsi_pkt_count_prev_total) *
571 ((double)rte_get_tsc_hz() / rdtsc_diff);
577 apply_traffic_profile(struct policy *pol)
583 diff = get_pkt_diff(pol);
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);
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);
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);
607 apply_time_profile(struct policy *pol)
613 char time_string[40];
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);
621 for (x = 0; x < RTE_POWER_HOURS_PER_DAY; x++) {
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);
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);
640 } else if (ptm->tm_hour ==
641 pol->pkt.timer_policy.hours_to_use_traffic_profile[x]) {
642 apply_traffic_profile(pol);
649 apply_workload_profile(struct policy *pol)
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);
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);
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);
676 apply_policy(struct policy *pol)
679 struct rte_power_channel_packet *pkt = &pol->pkt;
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);
691 write_binary_packet(void *buffer,
693 struct channel_info *chan_info)
697 if (buffer_len == 0 || buffer == NULL)
700 if (chan_info->fd < 0) {
701 RTE_LOG(ERR, CHANNEL_MONITOR, "Channel is not connected\n");
705 while (buffer_len > 0) {
706 ret = write(chan_info->fd, buffer, buffer_len);
710 RTE_LOG(ERR, CHANNEL_MONITOR, "Write function failed due to %s.\n",
714 buffer = (char *)buffer + ret;
721 send_freq(struct rte_power_channel_packet *pkt,
722 struct channel_info *chan_info,
725 unsigned int vcore_id = pkt->resource_id;
726 struct rte_power_channel_packet_freq_list channel_pkt_freq_list;
729 if (get_info_vm(pkt->vm_name, &info) != 0)
732 if (!freq_list && vcore_id >= RTE_POWER_MAX_VCPU_PER_VM)
735 if (!info.allow_query)
738 channel_pkt_freq_list.command = RTE_POWER_FREQ_LIST;
739 channel_pkt_freq_list.num_vcpu = info.num_vcpus;
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]);
747 channel_pkt_freq_list.freq_list[vcore_id] =
748 power_manager_get_current_frequency(info.pcpu_map[vcore_id]);
751 return write_binary_packet(&channel_pkt_freq_list,
752 sizeof(channel_pkt_freq_list),
757 send_capabilities(struct rte_power_channel_packet *pkt,
758 struct channel_info *chan_info,
761 unsigned int vcore_id = pkt->resource_id;
762 struct rte_power_channel_packet_caps_list channel_pkt_caps_list;
764 struct rte_power_core_capabilities caps;
767 if (get_info_vm(pkt->vm_name, &info) != 0)
770 if (!list_requested && vcore_id >= RTE_POWER_MAX_VCPU_PER_VM)
773 if (!info.allow_query)
776 channel_pkt_caps_list.command = RTE_POWER_CAPS_LIST;
777 channel_pkt_caps_list.num_vcpu = info.num_vcpus;
779 if (list_requested) {
781 for (i = 0; i < info.num_vcpus; i++) {
782 ret = rte_power_get_capabilities(info.pcpu_map[i],
785 channel_pkt_caps_list.turbo[i] =
787 channel_pkt_caps_list.priority[i] =
794 ret = rte_power_get_capabilities(info.pcpu_map[vcore_id],
797 channel_pkt_caps_list.turbo[vcore_id] =
799 channel_pkt_caps_list.priority[vcore_id] =
805 return write_binary_packet(&channel_pkt_caps_list,
806 sizeof(channel_pkt_caps_list),
811 send_ack_for_received_cmd(struct rte_power_channel_packet *pkt,
812 struct channel_info *chan_info,
815 pkt->command = command;
816 return write_binary_packet(pkt,
822 process_request(struct rte_power_channel_packet *pkt,
823 struct channel_info *chan_info)
827 if (chan_info == NULL)
830 if (rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_CONNECTED,
831 CHANNEL_MGR_CHANNEL_PROCESSING) == 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 rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_PROCESSING,
936 CHANNEL_MGR_CHANNEL_CONNECTED);
942 add_channel_to_monitor(struct channel_info **chan_info)
944 struct channel_info *info = *chan_info;
945 struct epoll_event event;
947 event.events = EPOLLIN;
948 event.data.ptr = info;
949 if (epoll_ctl(global_event_fd, EPOLL_CTL_ADD, info->fd, &event) < 0) {
950 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to add channel '%s' "
951 "to epoll\n", info->channel_path);
954 RTE_LOG(ERR, CHANNEL_MONITOR, "Added channel '%s' "
955 "to monitor\n", info->channel_path);
960 remove_channel_from_monitor(struct channel_info *chan_info)
962 if (epoll_ctl(global_event_fd, EPOLL_CTL_DEL,
963 chan_info->fd, NULL) < 0) {
964 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to remove channel '%s' "
965 "from epoll\n", chan_info->channel_path);
972 channel_monitor_init(void)
974 global_event_fd = epoll_create1(0);
975 if (global_event_fd == 0) {
976 RTE_LOG(ERR, CHANNEL_MONITOR,
977 "Error creating epoll context with error %s\n",
981 global_events_list = rte_malloc("epoll_events",
982 sizeof(*global_events_list)
983 * MAX_EVENTS, RTE_CACHE_LINE_SIZE);
984 if (global_events_list == NULL) {
985 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to rte_malloc for "
993 read_binary_packet(struct channel_info *chan_info)
995 struct rte_power_channel_packet pkt;
997 int buffer_len = sizeof(pkt);
998 int n_bytes, err = 0;
1000 while (buffer_len > 0) {
1001 n_bytes = read(chan_info->fd,
1002 buffer, buffer_len);
1003 if (n_bytes == buffer_len)
1007 RTE_LOG(DEBUG, CHANNEL_MONITOR,
1008 "Received error on "
1009 "channel '%s' read: %s\n",
1010 chan_info->channel_path,
1012 remove_channel(&chan_info);
1015 buffer = (char *)buffer + n_bytes;
1016 buffer_len -= n_bytes;
1019 process_request(&pkt, chan_info);
1024 read_json_packet(struct channel_info *chan_info)
1026 struct rte_power_channel_packet pkt;
1030 const char *resource_name;
1035 /* read opening brace to closing brace */
1040 n_bytes = read(chan_info->fd, &json_data[idx], 1);
1043 if (json_data[idx] == '{')
1045 if (json_data[idx] == '}')
1047 if ((indent > 0) || (idx > 0))
1051 if (idx >= MAX_JSON_STRING_LEN-1)
1053 } while (indent > 0);
1055 json_data[idx] = '\0';
1057 if (strlen(json_data) == 0)
1060 printf("got [%s]\n", json_data);
1062 root = json_loads(json_data, 0, &error);
1065 resource_name = get_resource_name_from_chn_path(
1066 chan_info->channel_path);
1068 * Because our data is now in the json
1069 * object, we can overwrite the pkt
1070 * with a rte_power_channel_packet struct, using
1071 * parse_json_to_pkt()
1073 ret = parse_json_to_pkt(root, &pkt, resource_name);
1076 RTE_LOG(ERR, CHANNEL_MONITOR,
1077 "Error validating JSON profile data\n");
1080 start = strstr(pkt.vm_name,
1081 CHANNEL_MGR_FIFO_PATTERN_NAME);
1082 if (start != NULL) {
1083 /* move past pattern to start of fifo id */
1084 start += strlen(CHANNEL_MGR_FIFO_PATTERN_NAME);
1087 n = (uint32_t)strtoul(start, &end, 10);
1089 if (end[0] == '\0') {
1090 /* Add core id to core list */
1092 pkt.vcpu_to_control[0] = n;
1093 process_request(&pkt, chan_info);
1095 RTE_LOG(ERR, CHANNEL_MONITOR,
1096 "Cannot extract core id from fifo name\n");
1099 process_request(&pkt, chan_info);
1102 RTE_LOG(ERR, CHANNEL_MONITOR,
1103 "JSON error on line %d: %s\n",
1104 error.line, error.text);
1106 } while (n_bytes > 0);
1111 run_channel_monitor(void)
1116 n_events = epoll_wait(global_event_fd, global_events_list,
1120 for (i = 0; i < n_events; i++) {
1121 struct channel_info *chan_info = (struct channel_info *)
1122 global_events_list[i].data.ptr;
1123 if ((global_events_list[i].events & EPOLLERR) ||
1124 (global_events_list[i].events & EPOLLHUP)) {
1125 RTE_LOG(INFO, CHANNEL_MONITOR,
1126 "Remote closed connection for "
1128 chan_info->channel_path);
1129 remove_channel(&chan_info);
1132 if (global_events_list[i].events & EPOLLIN) {
1134 switch (chan_info->type) {
1135 case CHANNEL_TYPE_BINARY:
1136 read_binary_packet(chan_info);
1139 case CHANNEL_TYPE_JSON:
1140 read_json_packet(chan_info);
1148 rte_delay_us(time_period_ms*1000);
1149 if (policy_is_set) {
1152 for (j = 0; j < RTE_DIM(policies); j++) {
1153 if (policies[j].enabled == 1)
1154 apply_policy(&policies[j]);