net: add macro to extract MAC address bytes
[dpdk.git] / examples / vm_power_manager / channel_monitor.c
index f600f4d..f03102e 100644 (file)
 #include <rte_atomic.h>
 #include <rte_cycles.h>
 #include <rte_ethdev.h>
-#ifdef RTE_LIBRTE_I40E_PMD
+#ifdef RTE_NET_I40E
 #include <rte_pmd_i40e.h>
 #endif
+#include <rte_power.h>
 
 #include <libvirt/libvirt.h>
 #include "channel_monitor.h"
-#include "channel_commands.h"
 #include "channel_manager.h"
 #include "power_manager.h"
 #include "oob_monitor.h"
@@ -107,7 +107,7 @@ str_to_ether_addr(const char *a, struct rte_ether_addr *ether_addr)
 }
 
 static int
-set_policy_mac(struct channel_packet *pkt, int idx, char *mac)
+set_policy_mac(struct rte_power_channel_packet *pkt, int idx, char *mac)
 {
        union PFID pfid;
        int ret;
@@ -124,9 +124,7 @@ set_policy_mac(struct channel_packet *pkt, int idx, char *mac)
 
        printf("Received MAC Address: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":"
                        "%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
-                       pfid.addr.addr_bytes[0], pfid.addr.addr_bytes[1],
-                       pfid.addr.addr_bytes[2], pfid.addr.addr_bytes[3],
-                       pfid.addr.addr_bytes[4], pfid.addr.addr_bytes[5]);
+                       RTE_ETHER_ADDR_BYTES(&pfid.addr));
 
        pkt->vfid[idx] = pfid.pfid;
        return 0;
@@ -164,7 +162,7 @@ get_resource_id_from_vmname(const char *vm_name)
 }
 
 static int
-parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
+parse_json_to_pkt(json_t *element, struct rte_power_channel_packet *pkt,
                                        const char *vm_name)
 {
        const char *key;
@@ -172,14 +170,14 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
        int ret;
        int resource_id;
 
-       memset(pkt, 0, sizeof(struct channel_packet));
+       memset(pkt, 0, sizeof(*pkt));
 
        pkt->nb_mac_to_monitor = 0;
        pkt->t_boost_status.tbEnabled = false;
-       pkt->workload = LOW;
-       pkt->policy_to_use = TIME;
-       pkt->command = PKT_POLICY;
-       pkt->core_type = CORE_TYPE_PHYSICAL;
+       pkt->workload = RTE_POWER_WL_LOW;
+       pkt->policy_to_use = RTE_POWER_POLICY_TIME;
+       pkt->command = RTE_POWER_PKT_POLICY;
+       pkt->core_type = RTE_POWER_CORE_TYPE_PHYSICAL;
 
        if (vm_name == NULL) {
                RTE_LOG(ERR, CHANNEL_MONITOR,
@@ -202,11 +200,11 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
                        char command[32];
                        strlcpy(command, json_string_value(value), 32);
                        if (!strcmp(command, "power")) {
-                               pkt->command = CPU_POWER;
+                               pkt->command = RTE_POWER_CPU_POWER;
                        } else if (!strcmp(command, "create")) {
-                               pkt->command = PKT_POLICY;
+                               pkt->command = RTE_POWER_PKT_POLICY;
                        } else if (!strcmp(command, "destroy")) {
-                               pkt->command = PKT_POLICY_REMOVE;
+                               pkt->command = RTE_POWER_PKT_POLICY_REMOVE;
                        } else {
                                RTE_LOG(ERR, CHANNEL_MONITOR,
                                        "Invalid command received in JSON\n");
@@ -216,13 +214,17 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
                        char command[32];
                        strlcpy(command, json_string_value(value), 32);
                        if (!strcmp(command, "TIME")) {
-                               pkt->policy_to_use = TIME;
+                               pkt->policy_to_use =
+                                               RTE_POWER_POLICY_TIME;
                        } else if (!strcmp(command, "TRAFFIC")) {
-                               pkt->policy_to_use = TRAFFIC;
+                               pkt->policy_to_use =
+                                               RTE_POWER_POLICY_TRAFFIC;
                        } else if (!strcmp(command, "WORKLOAD")) {
-                               pkt->policy_to_use = WORKLOAD;
+                               pkt->policy_to_use =
+                                               RTE_POWER_POLICY_WORKLOAD;
                        } else if (!strcmp(command, "BRANCH_RATIO")) {
-                               pkt->policy_to_use = BRANCH_RATIO;
+                               pkt->policy_to_use =
+                                               RTE_POWER_POLICY_BRANCH_RATIO;
                        } else {
                                RTE_LOG(ERR, CHANNEL_MONITOR,
                                        "Wrong policy_type received in JSON\n");
@@ -232,11 +234,11 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
                        char command[32];
                        strlcpy(command, json_string_value(value), 32);
                        if (!strcmp(command, "HIGH")) {
-                               pkt->workload = HIGH;
+                               pkt->workload = RTE_POWER_WL_HIGH;
                        } else if (!strcmp(command, "MEDIUM")) {
-                               pkt->workload = MEDIUM;
+                               pkt->workload = RTE_POWER_WL_MEDIUM;
                        } else if (!strcmp(command, "LOW")) {
-                               pkt->workload = LOW;
+                               pkt->workload = RTE_POWER_WL_LOW;
                        } else {
                                RTE_LOG(ERR, CHANNEL_MONITOR,
                                        "Wrong workload received in JSON\n");
@@ -282,17 +284,17 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
                        char unit[32];
                        strlcpy(unit, json_string_value(value), 32);
                        if (!strcmp(unit, "SCALE_UP")) {
-                               pkt->unit = CPU_POWER_SCALE_UP;
+                               pkt->unit = RTE_POWER_SCALE_UP;
                        } else if (!strcmp(unit, "SCALE_DOWN")) {
-                               pkt->unit = CPU_POWER_SCALE_DOWN;
+                               pkt->unit = RTE_POWER_SCALE_DOWN;
                        } else if (!strcmp(unit, "SCALE_MAX")) {
-                               pkt->unit = CPU_POWER_SCALE_MAX;
+                               pkt->unit = RTE_POWER_SCALE_MAX;
                        } else if (!strcmp(unit, "SCALE_MIN")) {
-                               pkt->unit = CPU_POWER_SCALE_MIN;
+                               pkt->unit = RTE_POWER_SCALE_MIN;
                        } else if (!strcmp(unit, "ENABLE_TURBO")) {
-                               pkt->unit = CPU_POWER_ENABLE_TURBO;
+                               pkt->unit = RTE_POWER_ENABLE_TURBO;
                        } else if (!strcmp(unit, "DISABLE_TURBO")) {
-                               pkt->unit = CPU_POWER_DISABLE_TURBO;
+                               pkt->unit = RTE_POWER_DISABLE_TURBO;
                        } else {
                                RTE_LOG(ERR, CHANNEL_MONITOR,
                                        "Invalid command received in JSON\n");
@@ -311,7 +313,7 @@ parse_json_to_pkt(json_t *element, struct channel_packet *pkt,
                                vm_name);
                        return -1;
                }
-               strlcpy(pkt->vm_name, vm_name, VM_MAX_NAME_SZ);
+               strlcpy(pkt->vm_name, vm_name, RTE_POWER_VM_MAX_NAME_SZ);
                pkt->resource_id = resource_id;
        }
        return 0;
@@ -366,7 +368,7 @@ pcpu_monitor(struct policy *pol, struct core_info *ci, int pcpu, int count)
 {
        int ret = 0;
 
-       if (pol->pkt.policy_to_use == BRANCH_RATIO) {
+       if (pol->pkt.policy_to_use == RTE_POWER_POLICY_BRANCH_RATIO) {
                ci->cd[pcpu].oob_enabled = 1;
                ret = add_core_to_monitor(pcpu);
                if (ret == 0)
@@ -406,7 +408,7 @@ get_pcpu_to_control(struct policy *pol)
         * differenciate between them when adding them to the branch monitor.
         * Virtual cores need to be converted to physical cores.
         */
-       if (pol->pkt.core_type == CORE_TYPE_VIRTUAL) {
+       if (pol->pkt.core_type == RTE_POWER_CORE_TYPE_VIRTUAL) {
                /*
                 * If the cores in the policy are virtual, we need to map them
                 * to physical core. We look up the vm info and use that for
@@ -438,7 +440,7 @@ get_pfid(struct policy *pol)
        for (i = 0; i < pol->pkt.nb_mac_to_monitor; i++) {
 
                RTE_ETH_FOREACH_DEV(x) {
-#ifdef RTE_LIBRTE_I40E_PMD
+#ifdef RTE_NET_I40E
                        ret = rte_pmd_i40e_query_vfid_by_mac(x,
                                (struct rte_ether_addr *)&(pol->pkt.vfid[i]));
 #else
@@ -462,7 +464,7 @@ get_pfid(struct policy *pol)
 }
 
 static int
-update_policy(struct channel_packet *pkt)
+update_policy(struct rte_power_channel_packet *pkt)
 {
 
        unsigned int updated = 0;
@@ -478,7 +480,8 @@ update_policy(struct channel_packet *pkt)
                        policies[i].pkt = *pkt;
                        get_pcpu_to_control(&policies[i]);
                        /* Check Eth dev only for Traffic policy */
-                       if (policies[i].pkt.policy_to_use == TRAFFIC) {
+                       if (policies[i].pkt.policy_to_use ==
+                                       RTE_POWER_POLICY_TRAFFIC) {
                                if (get_pfid(&policies[i]) < 0) {
                                        updated = 1;
                                        break;
@@ -495,7 +498,8 @@ update_policy(struct channel_packet *pkt)
                                policies[i].pkt = *pkt;
                                get_pcpu_to_control(&policies[i]);
                                /* Check Eth dev only for Traffic policy */
-                               if (policies[i].pkt.policy_to_use == TRAFFIC) {
+                               if (policies[i].pkt.policy_to_use ==
+                                               RTE_POWER_POLICY_TRAFFIC) {
                                        if (get_pfid(&policies[i]) < 0) {
                                                updated = 1;
                                                break;
@@ -511,7 +515,7 @@ update_policy(struct channel_packet *pkt)
 }
 
 static int
-remove_policy(struct channel_packet *pkt __rte_unused)
+remove_policy(struct rte_power_channel_packet *pkt __rte_unused)
 {
        unsigned int i;
 
@@ -537,13 +541,13 @@ get_pkt_diff(struct policy *pol)
                vsi_pkt_count_prev_total = 0;
        double rdtsc_curr, rdtsc_diff, diff;
        int x;
-#ifdef RTE_LIBRTE_I40E_PMD
+#ifdef RTE_NET_I40E
        struct rte_eth_stats vf_stats;
 #endif
 
        for (x = 0; x < pol->pkt.nb_mac_to_monitor; x++) {
 
-#ifdef RTE_LIBRTE_I40E_PMD
+#ifdef RTE_NET_I40E
                /*Read vsi stats*/
                if (rte_pmd_i40e_get_vf_stats(x, pol->pfid[x], &vf_stats) == 0)
                        vsi_pkt_count = vf_stats.ipackets;
@@ -614,7 +618,7 @@ apply_time_profile(struct policy *pol)
        /* Format the date and time, down to a single second. */
        strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);
 
-       for (x = 0; x < HOURS; x++) {
+       for (x = 0; x < RTE_POWER_HOURS_PER_DAY; x++) {
 
                if (ptm->tm_hour == pol->pkt.timer_policy.busy_hours[x]) {
                        for (count = 0; count < pol->pkt.num_vcpu; count++) {
@@ -647,19 +651,19 @@ apply_workload_profile(struct policy *pol)
 
        int count;
 
-       if (pol->pkt.workload == HIGH) {
+       if (pol->pkt.workload == RTE_POWER_WL_HIGH) {
                for (count = 0; count < pol->pkt.num_vcpu; count++) {
                        if (pol->core_share[count].status != 1)
                                power_manager_scale_core_max(
                                                pol->core_share[count].pcpu);
                }
-       } else if (pol->pkt.workload == MEDIUM) {
+       } else if (pol->pkt.workload == RTE_POWER_WL_MEDIUM) {
                for (count = 0; count < pol->pkt.num_vcpu; count++) {
                        if (pol->core_share[count].status != 1)
                                power_manager_scale_core_med(
                                                pol->core_share[count].pcpu);
                }
-       } else if (pol->pkt.workload == LOW) {
+       } else if (pol->pkt.workload == RTE_POWER_WL_LOW) {
                for (count = 0; count < pol->pkt.num_vcpu; count++) {
                        if (pol->core_share[count].status != 1)
                                power_manager_scale_core_min(
@@ -672,14 +676,14 @@ static void
 apply_policy(struct policy *pol)
 {
 
-       struct channel_packet *pkt = &pol->pkt;
+       struct rte_power_channel_packet *pkt = &pol->pkt;
 
        /*Check policy to use*/
-       if (pkt->policy_to_use == TRAFFIC)
+       if (pkt->policy_to_use == RTE_POWER_POLICY_TRAFFIC)
                apply_traffic_profile(pol);
-       else if (pkt->policy_to_use == TIME)
+       else if (pkt->policy_to_use == RTE_POWER_POLICY_TIME)
                apply_time_profile(pol);
-       else if (pkt->policy_to_use == WORKLOAD)
+       else if (pkt->policy_to_use == RTE_POWER_POLICY_WORKLOAD)
                apply_workload_profile(pol);
 }
 
@@ -714,21 +718,24 @@ write_binary_packet(void *buffer,
 }
 
 static int
-send_freq(struct channel_packet *pkt,
+send_freq(struct rte_power_channel_packet *pkt,
                struct channel_info *chan_info,
                bool freq_list)
 {
        unsigned int vcore_id = pkt->resource_id;
-       struct channel_packet_freq_list channel_pkt_freq_list;
+       struct rte_power_channel_packet_freq_list channel_pkt_freq_list;
        struct vm_info info;
 
        if (get_info_vm(pkt->vm_name, &info) != 0)
                return -1;
 
-       if (!freq_list && vcore_id >= MAX_VCPU_PER_VM)
+       if (!freq_list && vcore_id >= RTE_POWER_MAX_VCPU_PER_VM)
                return -1;
 
-       channel_pkt_freq_list.command = CPU_POWER_FREQ_LIST;
+       if (!info.allow_query)
+               return -1;
+
+       channel_pkt_freq_list.command = RTE_POWER_FREQ_LIST;
        channel_pkt_freq_list.num_vcpu = info.num_vcpus;
 
        if (freq_list) {
@@ -747,18 +754,73 @@ send_freq(struct channel_packet *pkt,
 }
 
 static int
-send_ack_for_received_cmd(struct channel_packet *pkt,
+send_capabilities(struct rte_power_channel_packet *pkt,
+               struct channel_info *chan_info,
+               bool list_requested)
+{
+       unsigned int vcore_id = pkt->resource_id;
+       struct rte_power_channel_packet_caps_list channel_pkt_caps_list;
+       struct vm_info info;
+       struct rte_power_core_capabilities caps;
+       int ret;
+
+       if (get_info_vm(pkt->vm_name, &info) != 0)
+               return -1;
+
+       if (!list_requested && vcore_id >= RTE_POWER_MAX_VCPU_PER_VM)
+               return -1;
+
+       if (!info.allow_query)
+               return -1;
+
+       channel_pkt_caps_list.command = RTE_POWER_CAPS_LIST;
+       channel_pkt_caps_list.num_vcpu = info.num_vcpus;
+
+       if (list_requested) {
+               unsigned int i;
+               for (i = 0; i < info.num_vcpus; i++) {
+                       ret = rte_power_get_capabilities(info.pcpu_map[i],
+                                       &caps);
+                       if (ret == 0) {
+                               channel_pkt_caps_list.turbo[i] =
+                                               caps.turbo;
+                               channel_pkt_caps_list.priority[i] =
+                                               caps.priority;
+                       } else
+                               return -1;
+
+               }
+       } else {
+               ret = rte_power_get_capabilities(info.pcpu_map[vcore_id],
+                               &caps);
+               if (ret == 0) {
+                       channel_pkt_caps_list.turbo[vcore_id] =
+                                       caps.turbo;
+                       channel_pkt_caps_list.priority[vcore_id] =
+                                       caps.priority;
+               } else
+                       return -1;
+       }
+
+       return write_binary_packet(&channel_pkt_caps_list,
+                       sizeof(channel_pkt_caps_list),
+                       chan_info);
+}
+
+static int
+send_ack_for_received_cmd(struct rte_power_channel_packet *pkt,
                struct channel_info *chan_info,
                uint32_t command)
 {
        pkt->command = command;
        return write_binary_packet(pkt,
-                       sizeof(struct channel_packet),
+                       sizeof(*pkt),
                        chan_info);
 }
 
 static int
-process_request(struct channel_packet *pkt, struct channel_info *chan_info)
+process_request(struct rte_power_channel_packet *pkt,
+               struct channel_info *chan_info)
 {
        int ret;
 
@@ -769,10 +831,10 @@ process_request(struct channel_packet *pkt, struct channel_info *chan_info)
                        CHANNEL_MGR_CHANNEL_PROCESSING) == 0)
                return -1;
 
-       if (pkt->command == CPU_POWER) {
+       if (pkt->command == RTE_POWER_CPU_POWER) {
                unsigned int core_num;
 
-               if (pkt->core_type == CORE_TYPE_VIRTUAL)
+               if (pkt->core_type == RTE_POWER_CORE_TYPE_VIRTUAL)
                        core_num = get_pcpu(chan_info, pkt->resource_id);
                else
                        core_num = pkt->resource_id;
@@ -784,22 +846,22 @@ process_request(struct channel_packet *pkt, struct channel_info *chan_info)
                bool valid_unit = true;
 
                switch (pkt->unit) {
-               case(CPU_POWER_SCALE_MIN):
+               case(RTE_POWER_SCALE_MIN):
                        scale_res = power_manager_scale_core_min(core_num);
                        break;
-               case(CPU_POWER_SCALE_MAX):
+               case(RTE_POWER_SCALE_MAX):
                        scale_res = power_manager_scale_core_max(core_num);
                        break;
-               case(CPU_POWER_SCALE_DOWN):
+               case(RTE_POWER_SCALE_DOWN):
                        scale_res = power_manager_scale_core_down(core_num);
                        break;
-               case(CPU_POWER_SCALE_UP):
+               case(RTE_POWER_SCALE_UP):
                        scale_res = power_manager_scale_core_up(core_num);
                        break;
-               case(CPU_POWER_ENABLE_TURBO):
+               case(RTE_POWER_ENABLE_TURBO):
                        scale_res = power_manager_enable_turbo_core(core_num);
                        break;
-               case(CPU_POWER_DISABLE_TURBO):
+               case(RTE_POWER_DISABLE_TURBO):
                        scale_res = power_manager_disable_turbo_core(core_num);
                        break;
                default:
@@ -810,9 +872,9 @@ process_request(struct channel_packet *pkt, struct channel_info *chan_info)
                if (valid_unit) {
                        ret = send_ack_for_received_cmd(pkt,
                                        chan_info,
-                                       scale_res > 0 ?
-                                               CPU_POWER_CMD_ACK :
-                                               CPU_POWER_CMD_NACK);
+                                       scale_res >= 0 ?
+                                               RTE_POWER_CMD_ACK :
+                                               RTE_POWER_CMD_NACK);
                        if (ret < 0)
                                RTE_LOG(ERR, CHANNEL_MONITOR, "Error during sending ack command.\n");
                } else
@@ -820,19 +882,19 @@ process_request(struct channel_packet *pkt, struct channel_info *chan_info)
 
        }
 
-       if (pkt->command == PKT_POLICY) {
+       if (pkt->command == RTE_POWER_PKT_POLICY) {
                RTE_LOG(INFO, CHANNEL_MONITOR, "Processing policy request %s\n",
                                pkt->vm_name);
                int ret = send_ack_for_received_cmd(pkt,
                                chan_info,
-                               CPU_POWER_CMD_ACK);
+                               RTE_POWER_CMD_ACK);
                if (ret < 0)
                        RTE_LOG(ERR, CHANNEL_MONITOR, "Error during sending ack command.\n");
                update_policy(pkt);
                policy_is_set = 1;
        }
 
-       if (pkt->command == PKT_POLICY_REMOVE) {
+       if (pkt->command == RTE_POWER_PKT_POLICY_REMOVE) {
                ret = remove_policy(pkt);
                if (ret == 0)
                        RTE_LOG(INFO, CHANNEL_MONITOR,
@@ -842,18 +904,30 @@ process_request(struct channel_packet *pkt, struct channel_info *chan_info)
                                 "Policy %s does not exist\n", pkt->vm_name);
        }
 
-       if (pkt->command == CPU_POWER_QUERY_FREQ_LIST ||
-               pkt->command == CPU_POWER_QUERY_FREQ) {
+       if (pkt->command == RTE_POWER_QUERY_FREQ_LIST ||
+               pkt->command == RTE_POWER_QUERY_FREQ) {
 
                RTE_LOG(INFO, CHANNEL_MONITOR,
                        "Frequency for %s requested.\n", pkt->vm_name);
                int ret = send_freq(pkt,
                                chan_info,
-                               pkt->command == CPU_POWER_QUERY_FREQ_LIST);
+                               pkt->command == RTE_POWER_QUERY_FREQ_LIST);
                if (ret < 0)
                        RTE_LOG(ERR, CHANNEL_MONITOR, "Error during frequency sending.\n");
        }
 
+       if (pkt->command == RTE_POWER_QUERY_CAPS_LIST ||
+               pkt->command == RTE_POWER_QUERY_CAPS) {
+
+               RTE_LOG(INFO, CHANNEL_MONITOR,
+                       "Capabilities for %s requested.\n", pkt->vm_name);
+               int ret = send_capabilities(pkt,
+                               chan_info,
+                               pkt->command == RTE_POWER_QUERY_CAPS_LIST);
+               if (ret < 0)
+                       RTE_LOG(ERR, CHANNEL_MONITOR, "Error during sending capabilities.\n");
+       }
+
        /*
         * Return is not checked as channel status may have been set to DISABLED
         * from management thread
@@ -918,7 +992,7 @@ channel_monitor_init(void)
 static void
 read_binary_packet(struct channel_info *chan_info)
 {
-       struct channel_packet pkt;
+       struct rte_power_channel_packet pkt;
        void *buffer = &pkt;
        int buffer_len = sizeof(pkt);
        int n_bytes, err = 0;
@@ -949,7 +1023,7 @@ read_binary_packet(struct channel_info *chan_info)
 static void
 read_json_packet(struct channel_info *chan_info)
 {
-       struct channel_packet pkt;
+       struct rte_power_channel_packet pkt;
        int n_bytes, ret;
        json_t *root;
        json_error_t error;
@@ -993,7 +1067,7 @@ read_json_packet(struct channel_info *chan_info)
                        /*
                         * Because our data is now in the json
                         * object, we can overwrite the pkt
-                        * with a channel_packet struct, using
+                        * with a rte_power_channel_packet struct, using
                         * parse_json_to_pkt()
                         */
                        ret = parse_json_to_pkt(root, &pkt, resource_name);