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>
31 #include <rte_pmd_i40e.h>
33 #include <libvirt/libvirt.h>
34 #include "channel_monitor.h"
35 #include "channel_commands.h"
36 #include "channel_manager.h"
37 #include "power_manager.h"
38 #include "oob_monitor.h"
40 #define RTE_LOGTYPE_CHANNEL_MONITOR RTE_LOGTYPE_USER1
42 #define MAX_EVENTS 256
44 uint64_t vsi_pkt_count_prev[384];
45 uint64_t rdtsc_prev[384];
46 #define MAX_JSON_STRING_LEN 1024
47 char json_data[MAX_JSON_STRING_LEN];
49 double time_period_ms = 1;
50 static volatile unsigned run_loop = 1;
51 static int global_event_fd;
52 static unsigned int policy_is_set;
53 static struct epoll_event *global_events_list;
54 static struct policy policies[MAX_CLIENTS];
59 struct ether_addr addr;
64 str_to_ether_addr(const char *a, struct ether_addr *ether_addr)
68 unsigned long o[ETHER_ADDR_LEN];
73 o[i] = strtoul(a, &end, 16);
74 if (errno != 0 || end == a || (end[0] != ':' && end[0] != 0))
77 } while (++i != RTE_DIM(o) / sizeof(o[0]) && end[0] != 0);
79 /* Junk at the end of line */
83 /* Support the format XX:XX:XX:XX:XX:XX */
84 if (i == ETHER_ADDR_LEN) {
88 ether_addr->addr_bytes[i] = (uint8_t)o[i];
90 /* Support the format XXXX:XXXX:XXXX */
91 } else if (i == ETHER_ADDR_LEN / 2) {
93 if (o[i] > UINT16_MAX)
95 ether_addr->addr_bytes[i * 2] =
97 ether_addr->addr_bytes[i * 2 + 1] =
98 (uint8_t)(o[i] & 0xff);
108 set_policy_mac(struct channel_packet *pkt, int idx, char *mac)
113 /* Use port MAC address as the vfid */
114 ret = str_to_ether_addr(mac, &pfid.addr);
117 RTE_LOG(ERR, CHANNEL_MONITOR,
118 "Invalid mac address received in JSON\n");
123 printf("Received MAC Address: %02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":"
124 "%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 "\n",
125 pfid.addr.addr_bytes[0], pfid.addr.addr_bytes[1],
126 pfid.addr.addr_bytes[2], pfid.addr.addr_bytes[3],
127 pfid.addr.addr_bytes[4], pfid.addr.addr_bytes[5]);
129 pkt->vfid[idx] = pfid.pfid;
135 parse_json_to_pkt(json_t *element, struct channel_packet *pkt)
141 memset(pkt, 0, sizeof(struct channel_packet));
143 pkt->nb_mac_to_monitor = 0;
144 pkt->t_boost_status.tbEnabled = false;
146 pkt->policy_to_use = TIME;
147 pkt->command = PKT_POLICY;
148 pkt->core_type = CORE_TYPE_PHYSICAL;
150 json_object_foreach(element, key, value) {
151 if (!strcmp(key, "policy")) {
152 /* Recurse in to get the contents of profile */
153 ret = parse_json_to_pkt(value, pkt);
156 } else if (!strcmp(key, "instruction")) {
157 /* Recurse in to get the contents of instruction */
158 ret = parse_json_to_pkt(value, pkt);
161 } else if (!strcmp(key, "name")) {
162 strcpy(pkt->vm_name, json_string_value(value));
163 } else if (!strcmp(key, "command")) {
165 strlcpy(command, json_string_value(value), 32);
166 if (!strcmp(command, "power")) {
167 pkt->command = CPU_POWER;
168 } else if (!strcmp(command, "create")) {
169 pkt->command = PKT_POLICY;
170 } else if (!strcmp(command, "destroy")) {
171 pkt->command = PKT_POLICY_REMOVE;
173 RTE_LOG(ERR, CHANNEL_MONITOR,
174 "Invalid command received in JSON\n");
177 } else if (!strcmp(key, "policy_type")) {
179 strlcpy(command, json_string_value(value), 32);
180 if (!strcmp(command, "TIME")) {
181 pkt->policy_to_use = TIME;
182 } else if (!strcmp(command, "TRAFFIC")) {
183 pkt->policy_to_use = TRAFFIC;
184 } else if (!strcmp(command, "WORKLOAD")) {
185 pkt->policy_to_use = WORKLOAD;
186 } else if (!strcmp(command, "BRANCH_RATIO")) {
187 pkt->policy_to_use = BRANCH_RATIO;
189 RTE_LOG(ERR, CHANNEL_MONITOR,
190 "Wrong policy_type received in JSON\n");
193 } else if (!strcmp(key, "workload")) {
195 strlcpy(command, json_string_value(value), 32);
196 if (!strcmp(command, "HIGH")) {
197 pkt->workload = HIGH;
198 } else if (!strcmp(command, "MEDIUM")) {
199 pkt->workload = MEDIUM;
200 } else if (!strcmp(command, "LOW")) {
203 RTE_LOG(ERR, CHANNEL_MONITOR,
204 "Wrong workload received in JSON\n");
207 } else if (!strcmp(key, "busy_hours")) {
209 size_t size = json_array_size(value);
211 for (i = 0; i < size; i++) {
212 int hour = (int)json_integer_value(
213 json_array_get(value, i));
214 pkt->timer_policy.busy_hours[i] = hour;
216 } else if (!strcmp(key, "quiet_hours")) {
218 size_t size = json_array_size(value);
220 for (i = 0; i < size; i++) {
221 int hour = (int)json_integer_value(
222 json_array_get(value, i));
223 pkt->timer_policy.quiet_hours[i] = hour;
225 } else if (!strcmp(key, "core_list")) {
227 size_t size = json_array_size(value);
229 for (i = 0; i < size; i++) {
230 int core = (int)json_integer_value(
231 json_array_get(value, i));
232 pkt->vcpu_to_control[i] = core;
234 pkt->num_vcpu = size;
235 } else if (!strcmp(key, "mac_list")) {
237 size_t size = json_array_size(value);
239 for (i = 0; i < size; i++) {
242 json_string_value(json_array_get(value, i)),
244 set_policy_mac(pkt, i, mac);
246 pkt->nb_mac_to_monitor = size;
247 } else if (!strcmp(key, "avg_packet_thresh")) {
248 pkt->traffic_policy.avg_max_packet_thresh =
249 (uint32_t)json_integer_value(value);
250 } else if (!strcmp(key, "max_packet_thresh")) {
251 pkt->traffic_policy.max_max_packet_thresh =
252 (uint32_t)json_integer_value(value);
253 } else if (!strcmp(key, "unit")) {
255 strlcpy(unit, json_string_value(value), 32);
256 if (!strcmp(unit, "SCALE_UP")) {
257 pkt->unit = CPU_POWER_SCALE_UP;
258 } else if (!strcmp(unit, "SCALE_DOWN")) {
259 pkt->unit = CPU_POWER_SCALE_DOWN;
260 } else if (!strcmp(unit, "SCALE_MAX")) {
261 pkt->unit = CPU_POWER_SCALE_MAX;
262 } else if (!strcmp(unit, "SCALE_MIN")) {
263 pkt->unit = CPU_POWER_SCALE_MIN;
264 } else if (!strcmp(unit, "ENABLE_TURBO")) {
265 pkt->unit = CPU_POWER_ENABLE_TURBO;
266 } else if (!strcmp(unit, "DISABLE_TURBO")) {
267 pkt->unit = CPU_POWER_DISABLE_TURBO;
269 RTE_LOG(ERR, CHANNEL_MONITOR,
270 "Invalid command received in JSON\n");
273 } else if (!strcmp(key, "resource_id")) {
274 pkt->resource_id = (uint32_t)json_integer_value(value);
276 RTE_LOG(ERR, CHANNEL_MONITOR,
277 "Unknown key received in JSON string: %s\n",
285 void channel_monitor_exit(void)
288 rte_free(global_events_list);
292 core_share(int pNo, int z, int x, int t)
294 if (policies[pNo].core_share[z].pcpu == lvm_info[x].pcpus[t]) {
295 if (strcmp(policies[pNo].pkt.vm_name,
296 lvm_info[x].vm_name) != 0) {
297 policies[pNo].core_share[z].status = 1;
298 power_manager_scale_core_max(
299 policies[pNo].core_share[z].pcpu);
305 core_share_status(int pNo)
308 int noVms = 0, noVcpus = 0, z, x, t;
310 get_all_vm(&noVms, &noVcpus);
312 /* Reset Core Share Status. */
313 for (z = 0; z < noVcpus; z++)
314 policies[pNo].core_share[z].status = 0;
316 /* Foreach vcpu in a policy. */
317 for (z = 0; z < policies[pNo].pkt.num_vcpu; z++) {
318 /* Foreach VM on the platform. */
319 for (x = 0; x < noVms; x++) {
320 /* Foreach vcpu of VMs on platform. */
321 for (t = 0; t < lvm_info[x].num_cpus; t++)
322 core_share(pNo, z, x, t);
329 pcpu_monitor(struct policy *pol, struct core_info *ci, int pcpu, int count)
333 if (pol->pkt.policy_to_use == BRANCH_RATIO) {
334 ci->cd[pcpu].oob_enabled = 1;
335 ret = add_core_to_monitor(pcpu);
337 RTE_LOG(INFO, CHANNEL_MONITOR,
338 "Monitoring pcpu %d OOB for %s\n",
339 pcpu, pol->pkt.vm_name);
341 RTE_LOG(ERR, CHANNEL_MONITOR,
342 "Error monitoring pcpu %d OOB for %s\n",
343 pcpu, pol->pkt.vm_name);
346 pol->core_share[count].pcpu = pcpu;
347 RTE_LOG(INFO, CHANNEL_MONITOR,
348 "Monitoring pcpu %d for %s\n",
349 pcpu, pol->pkt.vm_name);
355 get_pcpu_to_control(struct policy *pol)
358 /* Convert vcpu to pcpu. */
361 struct core_info *ci;
363 ci = get_core_info();
365 RTE_LOG(DEBUG, CHANNEL_MONITOR,
366 "Looking for pcpu for %s\n", pol->pkt.vm_name);
369 * So now that we're handling virtual and physical cores, we need to
370 * differenciate between them when adding them to the branch monitor.
371 * Virtual cores need to be converted to physical cores.
373 if (pol->pkt.core_type == CORE_TYPE_VIRTUAL) {
375 * If the cores in the policy are virtual, we need to map them
376 * to physical core. We look up the vm info and use that for
379 get_info_vm(pol->pkt.vm_name, &info);
380 for (count = 0; count < pol->pkt.num_vcpu; count++) {
381 pcpu = info.pcpu_map[pol->pkt.vcpu_to_control[count]];
382 pcpu_monitor(pol, ci, pcpu, count);
386 * If the cores in the policy are physical, we just use
387 * those core id's directly.
389 for (count = 0; count < pol->pkt.num_vcpu; count++) {
390 pcpu = pol->pkt.vcpu_to_control[count];
391 pcpu_monitor(pol, ci, pcpu, count);
397 get_pfid(struct policy *pol)
402 for (i = 0; i < pol->pkt.nb_mac_to_monitor; i++) {
404 RTE_ETH_FOREACH_DEV(x) {
405 ret = rte_pmd_i40e_query_vfid_by_mac(x,
406 (struct ether_addr *)&(pol->pkt.vfid[i]));
407 if (ret != -EINVAL) {
412 if (ret == -EINVAL || ret == -ENOTSUP || ret == ENODEV) {
413 RTE_LOG(INFO, CHANNEL_MONITOR,
414 "Error with Policy. MAC not found on "
425 update_policy(struct channel_packet *pkt)
428 unsigned int updated = 0;
432 RTE_LOG(INFO, CHANNEL_MONITOR,
433 "Applying policy for %s\n", pkt->vm_name);
435 for (i = 0; i < MAX_CLIENTS; i++) {
436 if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
437 /* Copy the contents of *pkt into the policy.pkt */
438 policies[i].pkt = *pkt;
439 get_pcpu_to_control(&policies[i]);
440 /* Check Eth dev only for Traffic policy */
441 if (policies[i].pkt.policy_to_use == TRAFFIC) {
442 if (get_pfid(&policies[i]) < 0) {
447 core_share_status(i);
448 policies[i].enabled = 1;
453 for (i = 0; i < MAX_CLIENTS; i++) {
454 if (policies[i].enabled == 0) {
455 policies[i].pkt = *pkt;
456 get_pcpu_to_control(&policies[i]);
457 /* Check Eth dev only for Traffic policy */
458 if (policies[i].pkt.policy_to_use == TRAFFIC) {
459 if (get_pfid(&policies[i]) < 0) {
464 core_share_status(i);
465 policies[i].enabled = 1;
474 remove_policy(struct channel_packet *pkt __rte_unused)
479 * Disabling the policy is simply a case of setting
482 for (i = 0; i < MAX_CLIENTS; i++) {
483 if (strcmp(policies[i].pkt.vm_name, pkt->vm_name) == 0) {
484 policies[i].enabled = 0;
492 get_pkt_diff(struct policy *pol)
495 uint64_t vsi_pkt_count,
497 vsi_pkt_count_prev_total = 0;
498 double rdtsc_curr, rdtsc_diff, diff;
500 struct rte_eth_stats vf_stats;
502 for (x = 0; x < pol->pkt.nb_mac_to_monitor; x++) {
505 if (rte_pmd_i40e_get_vf_stats(x, pol->pfid[x], &vf_stats) == 0)
506 vsi_pkt_count = vf_stats.ipackets;
510 vsi_pkt_total += vsi_pkt_count;
512 vsi_pkt_count_prev_total += vsi_pkt_count_prev[pol->pfid[x]];
513 vsi_pkt_count_prev[pol->pfid[x]] = vsi_pkt_count;
516 rdtsc_curr = rte_rdtsc_precise();
517 rdtsc_diff = rdtsc_curr - rdtsc_prev[pol->pfid[x-1]];
518 rdtsc_prev[pol->pfid[x-1]] = rdtsc_curr;
520 diff = (vsi_pkt_total - vsi_pkt_count_prev_total) *
521 ((double)rte_get_tsc_hz() / rdtsc_diff);
527 apply_traffic_profile(struct policy *pol)
533 diff = get_pkt_diff(pol);
535 if (diff >= (pol->pkt.traffic_policy.max_max_packet_thresh)) {
536 for (count = 0; count < pol->pkt.num_vcpu; count++) {
537 if (pol->core_share[count].status != 1)
538 power_manager_scale_core_max(
539 pol->core_share[count].pcpu);
541 } else if (diff >= (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
542 for (count = 0; count < pol->pkt.num_vcpu; count++) {
543 if (pol->core_share[count].status != 1)
544 power_manager_scale_core_med(
545 pol->core_share[count].pcpu);
547 } else if (diff < (pol->pkt.traffic_policy.avg_max_packet_thresh)) {
548 for (count = 0; count < pol->pkt.num_vcpu; count++) {
549 if (pol->core_share[count].status != 1)
550 power_manager_scale_core_min(
551 pol->core_share[count].pcpu);
557 apply_time_profile(struct policy *pol)
563 char time_string[40];
565 /* Obtain the time of day, and convert it to a tm struct. */
566 gettimeofday(&tv, NULL);
567 ptm = localtime(&tv.tv_sec);
568 /* Format the date and time, down to a single second. */
569 strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", ptm);
571 for (x = 0; x < HOURS; x++) {
573 if (ptm->tm_hour == pol->pkt.timer_policy.busy_hours[x]) {
574 for (count = 0; count < pol->pkt.num_vcpu; count++) {
575 if (pol->core_share[count].status != 1) {
576 power_manager_scale_core_max(
577 pol->core_share[count].pcpu);
581 } else if (ptm->tm_hour ==
582 pol->pkt.timer_policy.quiet_hours[x]) {
583 for (count = 0; count < pol->pkt.num_vcpu; count++) {
584 if (pol->core_share[count].status != 1) {
585 power_manager_scale_core_min(
586 pol->core_share[count].pcpu);
590 } else if (ptm->tm_hour ==
591 pol->pkt.timer_policy.hours_to_use_traffic_profile[x]) {
592 apply_traffic_profile(pol);
599 apply_workload_profile(struct policy *pol)
604 if (pol->pkt.workload == HIGH) {
605 for (count = 0; count < pol->pkt.num_vcpu; count++) {
606 if (pol->core_share[count].status != 1)
607 power_manager_scale_core_max(
608 pol->core_share[count].pcpu);
610 } else if (pol->pkt.workload == MEDIUM) {
611 for (count = 0; count < pol->pkt.num_vcpu; count++) {
612 if (pol->core_share[count].status != 1)
613 power_manager_scale_core_med(
614 pol->core_share[count].pcpu);
616 } else if (pol->pkt.workload == LOW) {
617 for (count = 0; count < pol->pkt.num_vcpu; count++) {
618 if (pol->core_share[count].status != 1)
619 power_manager_scale_core_min(
620 pol->core_share[count].pcpu);
626 apply_policy(struct policy *pol)
629 struct channel_packet *pkt = &pol->pkt;
631 /*Check policy to use*/
632 if (pkt->policy_to_use == TRAFFIC)
633 apply_traffic_profile(pol);
634 else if (pkt->policy_to_use == TIME)
635 apply_time_profile(pol);
636 else if (pkt->policy_to_use == WORKLOAD)
637 apply_workload_profile(pol);
641 process_request(struct channel_packet *pkt, struct channel_info *chan_info)
645 if (chan_info == NULL)
648 if (rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_CONNECTED,
649 CHANNEL_MGR_CHANNEL_PROCESSING) == 0)
652 if (pkt->command == CPU_POWER) {
653 unsigned int core_num;
655 if (pkt->core_type == CORE_TYPE_VIRTUAL)
656 core_num = get_pcpu(chan_info, pkt->resource_id);
658 core_num = pkt->resource_id;
660 RTE_LOG(DEBUG, CHANNEL_MONITOR, "Processing requested cmd for cpu:%d\n",
664 case(CPU_POWER_SCALE_MIN):
665 power_manager_scale_core_min(core_num);
667 case(CPU_POWER_SCALE_MAX):
668 power_manager_scale_core_max(core_num);
670 case(CPU_POWER_SCALE_DOWN):
671 power_manager_scale_core_down(core_num);
673 case(CPU_POWER_SCALE_UP):
674 power_manager_scale_core_up(core_num);
676 case(CPU_POWER_ENABLE_TURBO):
677 power_manager_enable_turbo_core(core_num);
679 case(CPU_POWER_DISABLE_TURBO):
680 power_manager_disable_turbo_core(core_num);
687 if (pkt->command == PKT_POLICY) {
688 RTE_LOG(INFO, CHANNEL_MONITOR, "Processing policy request %s\n",
694 if (pkt->command == PKT_POLICY_REMOVE) {
695 ret = remove_policy(pkt);
697 RTE_LOG(INFO, CHANNEL_MONITOR,
698 "Removed policy %s\n", pkt->vm_name);
700 RTE_LOG(INFO, CHANNEL_MONITOR,
701 "Policy %s does not exist\n", pkt->vm_name);
705 * Return is not checked as channel status may have been set to DISABLED
706 * from management thread
708 rte_atomic32_cmpset(&(chan_info->status), CHANNEL_MGR_CHANNEL_PROCESSING,
709 CHANNEL_MGR_CHANNEL_CONNECTED);
715 add_channel_to_monitor(struct channel_info **chan_info)
717 struct channel_info *info = *chan_info;
718 struct epoll_event event;
720 event.events = EPOLLIN;
721 event.data.ptr = info;
722 if (epoll_ctl(global_event_fd, EPOLL_CTL_ADD, info->fd, &event) < 0) {
723 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to add channel '%s' "
724 "to epoll\n", info->channel_path);
727 RTE_LOG(ERR, CHANNEL_MONITOR, "Added channel '%s' "
728 "to monitor\n", info->channel_path);
733 remove_channel_from_monitor(struct channel_info *chan_info)
735 if (epoll_ctl(global_event_fd, EPOLL_CTL_DEL,
736 chan_info->fd, NULL) < 0) {
737 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to remove channel '%s' "
738 "from epoll\n", chan_info->channel_path);
745 channel_monitor_init(void)
747 global_event_fd = epoll_create1(0);
748 if (global_event_fd == 0) {
749 RTE_LOG(ERR, CHANNEL_MONITOR,
750 "Error creating epoll context with error %s\n",
754 global_events_list = rte_malloc("epoll_events",
755 sizeof(*global_events_list)
756 * MAX_EVENTS, RTE_CACHE_LINE_SIZE);
757 if (global_events_list == NULL) {
758 RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to rte_malloc for "
766 read_binary_packet(struct channel_info *chan_info)
768 struct channel_packet pkt;
770 int buffer_len = sizeof(pkt);
771 int n_bytes, err = 0;
773 while (buffer_len > 0) {
774 n_bytes = read(chan_info->fd,
776 if (n_bytes == buffer_len)
780 RTE_LOG(DEBUG, CHANNEL_MONITOR,
782 "channel '%s' read: %s\n",
783 chan_info->channel_path,
785 remove_channel(&chan_info);
788 buffer = (char *)buffer + n_bytes;
789 buffer_len -= n_bytes;
792 process_request(&pkt, chan_info);
797 read_json_packet(struct channel_info *chan_info)
799 struct channel_packet pkt;
804 /* read opening brace to closing brace */
809 n_bytes = read(chan_info->fd, &json_data[idx], 1);
812 if (json_data[idx] == '{')
814 if (json_data[idx] == '}')
816 if ((indent > 0) || (idx > 0))
820 if (idx >= MAX_JSON_STRING_LEN-1)
822 } while (indent > 0);
826 * We've broken out of the read loop without getting
827 * a closing brace, so throw away the data
831 if (strlen(json_data) == 0)
834 printf("got [%s]\n", json_data);
836 root = json_loads(json_data, 0, &error);
840 * Because our data is now in the json
841 * object, we can overwrite the pkt
842 * with a channel_packet struct, using
843 * parse_json_to_pkt()
845 ret = parse_json_to_pkt(root, &pkt);
848 RTE_LOG(ERR, CHANNEL_MONITOR,
849 "Error validating JSON profile data\n");
852 process_request(&pkt, chan_info);
854 RTE_LOG(ERR, CHANNEL_MONITOR,
855 "JSON error on line %d: %s\n",
856 error.line, error.text);
858 } while (n_bytes > 0);
863 run_channel_monitor(void)
868 n_events = epoll_wait(global_event_fd, global_events_list,
872 for (i = 0; i < n_events; i++) {
873 struct channel_info *chan_info = (struct channel_info *)
874 global_events_list[i].data.ptr;
875 if ((global_events_list[i].events & EPOLLERR) ||
876 (global_events_list[i].events & EPOLLHUP)) {
877 RTE_LOG(INFO, CHANNEL_MONITOR,
878 "Remote closed connection for "
880 chan_info->channel_path);
881 remove_channel(&chan_info);
884 if (global_events_list[i].events & EPOLLIN) {
886 switch (chan_info->type) {
887 case CHANNEL_TYPE_BINARY:
888 read_binary_packet(chan_info);
891 case CHANNEL_TYPE_JSON:
892 read_json_packet(chan_info);
900 rte_delay_us(time_period_ms*1000);
904 for (j = 0; j < MAX_CLIENTS; j++) {
905 if (policies[j].enabled == 1)
906 apply_policy(&policies[j]);