VM Power Management
PATCH v5 1/10 Channel Manager and Monitor for VM Power Management(Host).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
The manager is responsible for adding communications channels to the Monitor thread, tracking and reporting VM state and employs the libvirt API for synchronization with the KVM Hypervisor. The manager interacts with the Hypervisor to discover the mapping of virtual CPUS(vCPUs) to the host physical CPUS(pCPUs) and to inspect the VM running state.
The manager provides the following functionality to the CLI:
1) Connect to a libvirtd instance, default: qemu:///system
2) Add a VM to an internal list, each VM is identified by a "name" which must correspond a valid libvirt Domain Name.
3) Add communication channels associated with a VM to the epoll based Monitor thread.
The channels must exist and be in the form of:
/tmp/powermonitor/<vm_name>.<channel_number>. Each channel is a Virtio-Serial endpoint configured as an AF_UNIX file socket and opened in non-blocking mode.
Each VM can have a maximum of 64 channels associated with it.
4) Disable or re-enable VM communication channels, channels once added to the Monitor thread remain in that threads control, however acting on channel requests can be disabled and renabled via CLI.
The monitor is an epoll based infinite loop running in a separate thread that waits on channel events from VMs and calls the corresponding functions. Channel definitions from the manager are registered via the epoll event opaque pointer when calling epoll_ctl(EPOLL_CTL_ADD), this allows for obtaining the channels file descriptor for reading EPOLLIN events and mapping the vCPU to pCPU(s) associated with a request from a particular VM.
--- examples/vm_power_manager/channel_manager.c | 804 +++++++++++++++++++++++++++ examples/vm_power_manager/channel_manager.h | 314 +++++++++++ examples/vm_power_manager/channel_monitor.c | 231 ++++++++ examples/vm_power_manager/channel_monitor.h | 102 ++++ 4 files changed, 1451 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/channel_manager.c create mode 100644 examples/vm_power_manager/channel_manager.h create mode 100644 examples/vm_power_manager/channel_monitor.c create mode 100644 examples/vm_power_manager/channel_monitor.h
examples/vm_power_manager/channel_manager.c new file +804 -0
@@ -0,0 +1,804 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/un.h> +#include <fcntl.h> +#include <unistd.h> +#include <inttypes.h> +#include <dirent.h> +#include <errno.h> + +#include <sys/queue.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/select.h> + +#include <rte_config.h> +#include <rte_malloc.h> +#include <rte_memory.h> +#include <rte_mempool.h> +#include <rte_log.h> +#include <rte_atomic.h> +#include <rte_spinlock.h> + +#include <libvirt/libvirt.h> + +#include "channel_manager.h" +#include "channel_commands.h" +#include "channel_monitor.h" + + +#define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1 + +#define ITERATIVE_BITMASK_CHECK_64 (mask_u64b, i) \ + for (i = 0; mask_u64b; mask_u64b &= ~(1ULL << i++)) \ + if ((mask_u64b >> i) & 1) \ + +/* Global pointer to libvirt connection */ +static virConnectPtr global_vir_conn_ptr; + +static unsigned char *global_cpumaps; +static virVcpuInfo *global_vircpuinfo; +static size_t global_maplen; + +static unsigned global_n_host_cpus; + +/* +* Represents a single Virtual Machine +*/ +struct virtual_machine_info { +char name[CHANNEL_MGR_MAX_NAME_LEN ]; +rte_atomic64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS ]; +struct channel_info *channels[CHANNEL_CMDS_MAX_VM_CHANNELS ]; +uint64_t channel_mask; +uint8_t num_channels; +enum vm_status status; +virDomainPtr domainPtr; +virDomainInfo info; +rte_spinlock_t config_spinlock; +LIST_ENTRY (virtual_machine_info) vms_info; +}; + +LIST_HEAD(, virtual_machine_info) vm_list_head; + +static struct virtual_machine_info * +find_domain_by_name (const char *name) +{ +struct virtual_machine_info *info; + LIST_FOREACH(info, &vm_list_head, vms_info) { +if (!strncmp (info->name, name,CHANNEL_MGR_MAX_NAME_LEN -1 )) +return info; + } +return NULL ; +} + +static int +update_pcpus_mask (struct virtual_machine_info *vm_info) +{ +virVcpuInfoPtr cpuinfo; +unsigned i, j; +int n_vcpus; +uint64_t mask; + +memset (global_cpumaps,0 ,CHANNEL_CMDS_MAX_CPUS *global_maplen); + +if (!virDomainIsActive (vm_info->domainPtr)) { + n_vcpus =virDomainGetVcpuPinInfo (vm_info->domainPtr, + vm_info->info.nrVirtCpu, global_cpumaps, global_maplen, +VIR_DOMAIN_AFFECT_CONFIG ); +if (n_vcpus <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting vCPU info for " +"in-active VM '%s'\n" , vm_info->name); +return -1 ; + } + goto update_pcpus; + } + +memset (global_vircpuinfo,0 ,sizeof (*global_vircpuinfo)* +CHANNEL_CMDS_MAX_CPUS ); + + cpuinfo = global_vircpuinfo; + + n_vcpus =virDomainGetVcpus (vm_info->domainPtr, cpuinfo, +CHANNEL_CMDS_MAX_CPUS , global_cpumaps, global_maplen); +if (n_vcpus <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting vCPU info for " +"active VM '%s'\n" , vm_info->name); +return -1 ; + } +update_pcpus: +if (n_vcpus >=CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Number of vCPUS(%u) is out of range " +"0...%d\n" , n_vcpus,CHANNEL_CMDS_MAX_CPUS -1 ); +return -1 ; + } +if (n_vcpus != vm_info->info.nrVirtCpu) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Updating the number of vCPUs for VM '%s" +" from %d -> %d\n" , vm_info->name, vm_info->info.nrVirtCpu, + n_vcpus); + vm_info->info.nrVirtCpu = n_vcpus; + } +for (i =0 ; i < vm_info->info.nrVirtCpu; i++) { + mask =0 ; +for (j =0 ; j < global_n_host_cpus; j++) { +if (VIR_CPU_USABLE (global_cpumaps, global_maplen, i, j) >0 ) { + mask |=1ULL << j; + } + } +rte_atomic64_set (&vm_info->pcpu_mask[i], mask); + } +return 0 ; +} + +int +set_pcpus_mask (char *vm_name,unsigned vcpu,uint64_t core_mask) +{ +unsigned i =0 ; +int flags =VIR_DOMAIN_AFFECT_LIVE |VIR_DOMAIN_AFFECT_CONFIG ; +struct virtual_machine_info *vm_info; +uint64_t mask = core_mask; + +if (vcpu >=CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"vCPU(%u) exceeds max allowable(%d)\n" , + vcpu,CHANNEL_CMDS_MAX_CPUS -1 ); +return -1 ; + } + + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM '%s' not found\n" , vm_name); +return -1 ; + } + +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to set vCPU(%u) to pCPU " +"mask(0x%" PRIx64") for VM '%s', VM is not active\n" , + vcpu, core_mask, vm_info->name); +return -1 ; + } + +if (vcpu >= vm_info->info.nrVirtCpu) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"vCPU(%u) exceeds the assigned number of " +"vCPUs(%u)\n" , vcpu, vm_info->info.nrVirtCpu); +return -1 ; + } +memset (global_cpumaps,0 ,CHANNEL_CMDS_MAX_CPUS * global_maplen); + ITERATIVE_BITMASK_CHECK_64(mask, i) { +VIR_USE_CPU (global_cpumaps, i); +if (i >= global_n_host_cpus) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"CPU(%u) exceeds the available " +"number of CPUs(%u)\n" , i, global_n_host_cpus); +return -1 ; + } + } +if (virDomainPinVcpuFlags (vm_info->domainPtr, vcpu, global_cpumaps, + global_maplen, flags) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to set vCPU(%u) to pCPU " +"mask(0x%" PRIx64") for VM '%s'\n" , vcpu, core_mask, + vm_info->name); +return -1 ; + } +rte_atomic64_set (&vm_info->pcpu_mask[vcpu], core_mask); +return 0 ; + +} + +int +set_pcpu (char *vm_name,unsigned vcpu,unsigned core_num) +{ +uint64_t mask =1ULL << core_num; +return set_pcpus_mask (vm_name, vcpu, mask); +} + +uint64_t +get_pcpus_mask (struct channel_info *chan_info,unsigned vcpu) +{ +struct virtual_machine_info *vm_info = + (struct virtual_machine_info *)chan_info->priv_info; +return rte_atomic64_read (&vm_info->pcpu_mask[vcpu]); +} + +static inline int +channel_exists (struct virtual_machine_info *vm_info,unsigned channel_num) +{ +rte_spinlock_lock (&(vm_info->config_spinlock)); +if (vm_info->channel_mask & (1ULL << channel_num)) { +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 1 ; + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 0 ; +} + + + +static int +open_non_blocking_channel (struct channel_info *info) +{ +int ret, flags; +struct sockaddr_un sock_addr; +fd_set soc_fd_set; +struct timeval tv; + + info->fd =socket (AF_UNIX ,SOCK_STREAM ,0 ); +if (info->fd ==-1 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error(%s) creating socket for '%s'\n" , +strerror (errno), + info->channel_path); +return -1 ; + } + sock_addr.sun_family =AF_UNIX ; +memcpy (&sock_addr.sun_path, info->channel_path, +strlen (info->channel_path)+1 ); + +/* Get current flags */ + flags =fcntl (info->fd,F_GETFL ,0 ); +if (flags <0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) fcntl get flags socket for" +"'%s'\n" ,strerror (errno), info->channel_path); +return 1 ; + } +/* Set to Non Blocking */ + flags |=O_NONBLOCK ; +if (fcntl (info->fd,F_SETFL , flags) <0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) setting non-blocking " +"socket for '%s'\n" ,strerror (errno), info->channel_path); +return -1 ; + } + ret =connect (info->fd, (struct sockaddr *)&sock_addr, +sizeof (sock_addr)); +if (ret <0 ) { +/* ECONNREFUSED error is given when VM is not active */ +if (errno ==ECONNREFUSED ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"VM is not active or has not " +"activated its endpoint to channel %s\n" , + info->channel_path); +return -1 ; + } +/* Wait for tv_sec if in progress */ +else if (errno ==EINPROGRESS ) { + tv.tv_sec =2 ; + tv.tv_usec =0 ; +FD_ZERO (&soc_fd_set); +FD_SET (info->fd, &soc_fd_set); +if (select (info->fd+1 ,NULL , &soc_fd_set,NULL , &tv) >0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Timeout or error on channel " +"'%s'\n" , info->channel_path); +return -1 ; + } + }else { +/* Any other error */ +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) connecting socket" +" for '%s'\n" ,strerror (errno), info->channel_path); +return -1 ; + } + } +return 0 ; +} + +static int +setup_channel_info (struct virtual_machine_info **vm_info_dptr, +struct channel_info **chan_info_dptr,unsigned channel_num) +{ +struct channel_info *chan_info = *chan_info_dptr; +struct virtual_machine_info *vm_info = *vm_info_dptr; + + chan_info->channel_num = channel_num; + chan_info->priv_info = (void *)vm_info; + chan_info->status =CHANNEL_MGR_CHANNEL_DISCONNECTED ; +if (open_non_blocking_channel (chan_info) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Could not open channel: " +"'%s' for VM '%s'\n" , + chan_info->channel_path, vm_info->name); +return -1 ; + } +if (add_channel_to_monitor (&chan_info) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Could add channel: " +"'%s' to epoll ctl for VM '%s'\n" , + chan_info->channel_path, vm_info->name); +return -1 ; + + } +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->num_channels++; + vm_info->channel_mask |=1ULL << channel_num; + vm_info->channels[channel_num] = chan_info; + chan_info->status =CHANNEL_MGR_CHANNEL_CONNECTED ; +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 0 ; +} + +int +add_all_channels (const char *vm_name) +{ +DIR *d; +struct dirent *dir; +struct virtual_machine_info *vm_info; +struct channel_info *chan_info; +char *token, *remaining, *tail_ptr; +char socket_name[PATH_MAX ]; +unsigned channel_num; +int num_channels_enabled =0 ; + +/* verify VM exists */ + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' not found" +" during channel discovery\n" , vm_name); +return 0 ; + } +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' is not active\n" , vm_name); + vm_info->status =CHANNEL_MGR_VM_INACTIVE ; +return 0 ; + } + d =opendir (CHANNEL_MGR_SOCKET_PATH ); +if (d ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error opening directory '%s': %s\n" , +CHANNEL_MGR_SOCKET_PATH ,strerror (errno)); +return -1 ; + } +while ((dir =readdir (d)) !=NULL ) { +if (!strncmp (dir->d_name,"." ,1 ) || + !strncmp (dir->d_name,".." ,2 )) +continue ; + +snprintf (socket_name,sizeof (socket_name),"%s" , dir->d_name); + remaining = socket_name; +/* Extract vm_name from "<vm_name>.<channel_num>" */ + token =strsep (&remaining,"." ); +if (remaining ==NULL ) +continue ; +if (strncmp (vm_name, token,CHANNEL_MGR_MAX_NAME_LEN )) +continue ; + +/* remaining should contain only <channel_num> */ + errno =0 ; + channel_num = (unsigned )strtol (remaining, &tail_ptr,0 ); +if ((errno !=0 ) || (remaining[0 ] =='\0' ) || + (*tail_ptr !='\0' ) || tail_ptr ==NULL ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Malformed channel name" +"'%s' found it should be in the form of " +"'<guest_name>.<channel_num>(decimal)'\n" , + dir->d_name); +continue ; + } +if (channel_num >=CHANNEL_CMDS_MAX_VM_CHANNELS ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Channel number(%u) is " +"greater than max allowable: %d, skipping '%s%s'\n" , + channel_num,CHANNEL_CMDS_MAX_VM_CHANNELS -1 , +CHANNEL_MGR_SOCKET_PATH , dir->d_name); +continue ; + } +/* if channel has not been added previously */ +if (channel_exists (vm_info, channel_num)) +continue ; + + chan_info =rte_malloc (NULL ,sizeof (*chan_info), +CACHE_LINE_SIZE ); +if (chan_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for " +"channel '%s%s'\n" ,CHANNEL_MGR_SOCKET_PATH , dir->d_name); +continue ; + } + +snprintf (chan_info->channel_path, +sizeof (chan_info->channel_path),"%s%s" , +CHANNEL_MGR_SOCKET_PATH , dir->d_name); + +if (setup_channel_info (&vm_info, &chan_info, channel_num) <0 ) { +rte_free (chan_info); +continue ; + } + + num_channels_enabled++; + } +closedir (d); +return num_channels_enabled; +} + +int +add_channels (const char *vm_name,unsigned *channel_list, +unsigned len_channel_list) +{ +struct virtual_machine_info *vm_info; +struct channel_info *chan_info; +char socket_path[PATH_MAX ]; +unsigned i; +int num_channels_enabled =0 ; + + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } + +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' is not active\n" , vm_name); + vm_info->status =CHANNEL_MGR_VM_INACTIVE ; +return 0 ; + } + +for (i =0 ; i < len_channel_list; i++) { + +if (channel_list[i] >=CHANNEL_CMDS_MAX_VM_CHANNELS ) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Channel(%u) is out of range " +"0...%d\n" , channel_list[i], +CHANNEL_CMDS_MAX_VM_CHANNELS -1 ); +continue ; + } +if (channel_exists (vm_info, channel_list[i])) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Channel already exists, skipping " +"'%s.%u'\n" , vm_name, i); +continue ; + } + +snprintf (socket_path,sizeof (socket_path),"%s%s.%u" , +CHANNEL_MGR_SOCKET_PATH , vm_name, channel_list[i]); + errno =0 ; +if (access (socket_path,F_OK ) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channel path '%s' error: " +"%s\n" , socket_path,strerror (errno)); +continue ; + } + chan_info =rte_malloc (NULL ,sizeof (*chan_info), +CACHE_LINE_SIZE ); +if (chan_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for " +"channel '%s'\n" , socket_path); +continue ; + } +snprintf (chan_info->channel_path, +sizeof (chan_info->channel_path),"%s%s.%u" , +CHANNEL_MGR_SOCKET_PATH , vm_name, channel_list[i]); +if (setup_channel_info (&vm_info, &chan_info, channel_list[i]) <0 ) { +rte_free (chan_info); +continue ; + } + num_channels_enabled++; + + } +return num_channels_enabled; +} + +int +remove_channel (struct channel_info **chan_info_dptr) +{ +struct virtual_machine_info *vm_info; +struct channel_info *chan_info = *chan_info_dptr; + +close (chan_info->fd); + + vm_info = (struct virtual_machine_info *)chan_info->priv_info; + +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->channel_mask &= ~(1ULL << chan_info->channel_num); + vm_info->num_channels--; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +rte_free (chan_info); +return 0 ; +} + +int +set_channel_status_all (const char *vm_name,enum channel_status status) +{ +struct virtual_machine_info *vm_info; +unsigned i; +uint64_t mask; +int num_channels_changed =0 ; + +if (!(status ==CHANNEL_MGR_CHANNEL_CONNECTED || + status ==CHANNEL_MGR_CHANNEL_DISABLED )) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channels can only be enabled or " +"disabled: Unable to change status for VM '%s'\n" , vm_name); + } + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to disable channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } + +rte_spinlock_lock (&(vm_info->config_spinlock)); + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { + vm_info->channels[i]->status = status; + num_channels_changed++; + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return num_channels_changed; + +} + +int +set_channel_status (const char *vm_name,unsigned *channel_list, +unsigned len_channel_list,enum channel_status status) +{ +struct virtual_machine_info *vm_info; +unsigned i; +int num_channels_changed =0 ; + +if (!(status ==CHANNEL_MGR_CHANNEL_CONNECTED || + status ==CHANNEL_MGR_CHANNEL_DISABLED )) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channels can only be enabled or " +"disabled: Unable to change status for VM '%s'\n" , vm_name); + } + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } +for (i =0 ; i < len_channel_list; i++) { +if (channel_exists (vm_info, channel_list[i])) { +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->channels[channel_list[i]]->status = status; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + num_channels_changed++; + } + } +return num_channels_changed; +} + +int +get_info_vm (const char *vm_name,struct vm_info *info) +{ +struct virtual_machine_info *vm_info; +unsigned i, channel_num =0 ; +uint64_t mask; + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM '%s' not found\n" , vm_name); +return -1 ; + } + info->status =CHANNEL_MGR_VM_ACTIVE ; +if (!virDomainIsActive (vm_info->domainPtr)) + info->status =CHANNEL_MGR_VM_INACTIVE ; + +rte_spinlock_lock (&(vm_info->config_spinlock)); + + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { + info->channels[channel_num].channel_num = i; +memcpy (info->channels[channel_num].channel_path, + vm_info->channels[i]->channel_path,PATH_MAX ); + info->channels[channel_num].status = vm_info->channels[i]->status; + info->channels[channel_num].fd = vm_info->channels[i]->fd; + channel_num++; + } + + info->num_channels = channel_num; + info->num_vcpus = vm_info->info.nrVirtCpu; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +memcpy (info->name, vm_info->name,sizeof (vm_info->name)); +for (i =0 ; i < info->num_vcpus; i++) { + info->pcpu_mask[i] =rte_atomic64_read (&vm_info->pcpu_mask[i]); + } +return 0 ; +} + +int +add_vm (const char *vm_name) +{ +struct virtual_machine_info *new_domain; +virDomainPtr dom_ptr; +int i; + +if (find_domain_by_name (vm_name) !=NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add VM: VM '%s' " +"already exists\n" , vm_name); +return -1 ; + } + +if (global_vir_conn_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"No connection to hypervisor exists\n" ); +return -1 ; + } + dom_ptr =virDomainLookupByName (global_vir_conn_ptr, vm_name); +if (dom_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error on VM lookup with libvirt: " +"VM '%s' not found\n" , vm_name); +return -1 ; + } + + new_domain =rte_malloc ("virtual_machine_info" ,sizeof (*new_domain), +CACHE_LINE_SIZE ); +if (new_domain ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to allocate memory for VM " +"info\n" ); +return -1 ; + } + new_domain->domainPtr = dom_ptr; +if (virDomainGetInfo (new_domain->domainPtr, &new_domain->info) !=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to get libvirt VM info\n" ); +rte_free (new_domain); +return -1 ; + } +if (new_domain->info.nrVirtCpu >CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error the number of virtual CPUs(%u) is " +"greater than allowable(%d)\n" , new_domain->info.nrVirtCpu, +CHANNEL_CMDS_MAX_CPUS ); +rte_free (new_domain); +return -1 ; + } + +for (i =0 ; i <CHANNEL_CMDS_MAX_CPUS ; i++) { +rte_atomic64_init (&new_domain->pcpu_mask[i]); + } +if (update_pcpus_mask (new_domain) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting physical CPU pinning\n" ); +rte_free (new_domain); +return -1 ; + } +strncpy (new_domain->name, vm_name,sizeof (new_domain->name)); + new_domain->channel_mask =0 ; + new_domain->num_channels =0 ; + +if (!virDomainIsActive (dom_ptr)) + new_domain->status =CHANNEL_MGR_VM_INACTIVE ; +else + new_domain->status =CHANNEL_MGR_VM_ACTIVE ; + +rte_spinlock_init (&(new_domain->config_spinlock)); +LIST_INSERT_HEAD (&vm_list_head, new_domain, vms_info); +return 0 ; +} + +int +remove_vm (const char *vm_name) +{ +struct virtual_machine_info *vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to remove VM: VM '%s' " +"not found\n" , vm_name); +return -1 ; + } +rte_spinlock_lock (&vm_info->config_spinlock); +if (vm_info->num_channels !=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to remove VM '%s', there are " +"%" PRId8" channels still active\n" , + vm_name, vm_info->num_channels); +rte_spinlock_unlock (&vm_info->config_spinlock); +return -1 ; + } +LIST_REMOVE (vm_info, vms_info); +rte_spinlock_unlock (&vm_info->config_spinlock); +rte_free (vm_info); +return 0 ; +} + +static void +disconnect_hypervisor (void ) +{ +if (global_vir_conn_ptr !=NULL ) { +virConnectClose (global_vir_conn_ptr); + global_vir_conn_ptr =NULL ; + } +} + +static int +connect_hypervisor (const char *path) +{ +if (global_vir_conn_ptr !=NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error connecting to %s, connection" +"already established\n" , path); +return -1 ; + } + global_vir_conn_ptr =virConnectOpen (path); +if (global_vir_conn_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error failed to open connection to " +"Hypervisor '%s'\n" , path); +return -1 ; + } +return 0 ; +} + +int +channel_manager_init (const char *path) +{ +int n_cpus; +LIST_INIT (&vm_list_head); +if (connect_hypervisor (path) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to initialize channel manager\n" ); +return -1 ; + } + + global_maplen =VIR_CPU_MAPLEN (CHANNEL_CMDS_MAX_CPUS ); + + global_vircpuinfo =rte_zmalloc (NULL ,sizeof (*global_vircpuinfo) * +CHANNEL_CMDS_MAX_CPUS ,CACHE_LINE_SIZE ); +if (global_vircpuinfo ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for CPU Info\n" ); + goto error; + } + global_cpumaps =rte_zmalloc (NULL ,CHANNEL_CMDS_MAX_CPUS * global_maplen, +CACHE_LINE_SIZE ); +if (global_cpumaps ==NULL ) { + goto error; + } + + n_cpus =virNodeGetCPUMap (global_vir_conn_ptr,NULL ,NULL ,0 ); +if (n_cpus <=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to get the number of Host " +"CPUs\n" ); + goto error; + } + global_n_host_cpus = (unsigned )n_cpus; + +if (global_n_host_cpus >CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"The number of host CPUs(%u) exceeds the " +"maximum of %u\n" , global_n_host_cpus,CHANNEL_CMDS_MAX_CPUS ); + goto error; + + } + +return 0 ; +error: +disconnect_hypervisor (); +return -1 ; +} + +void +channel_manager_exit (void ) +{ +unsigned i; +uint64_t mask; +struct virtual_machine_info *vm_info; + + LIST_FOREACH(vm_info, &vm_list_head, vms_info) { + +rte_spinlock_lock (&(vm_info->config_spinlock)); + + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { +remove_channel_from_monitor (vm_info->channels[i]); +close (vm_info->channels[i]->fd); +rte_free (vm_info->channels[i]); + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +LIST_REMOVE (vm_info, vms_info); +rte_free (vm_info); + } + +if (global_cpumaps !=NULL ) +rte_free (global_cpumaps); +if (global_vircpuinfo !=NULL ) +rte_free (global_vircpuinfo); +disconnect_hypervisor (); +}
examples/vm_power_manager/channel_manager.h new file +314 -0
314 lines not on this page · raw message
examples/vm_power_manager/channel_monitor.c new file +231 -0
231 lines not on this page · raw message
examples/vm_power_manager/channel_monitor.h new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v5 2/10 VM Power Management CLI(Host).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
The CLI is used for administrating the channel monitor and manager and manually setting the CPU frequency on the host.
Supports the following commands:
add_vm [Mul-choice STRING]: add_vm|rm_vm <name>, add a VM for subsequent
operations with the CLI or remove a previously added VM from the VM Power Manager
rm_vm [Mul-choice STRING]: add_vm|rm_vm <name>, add a VM for subsequent
operations with the CLI or remove a previously added VM from the VM Power Manager
add_channels [Fixed STRING]: add_channels <vm_name> <list>|all, add
communication channels for the specified VM, the virtio channels must be enabled in the VM configuration(qemu/libvirt) and the associated VM must be active. <list> is a comma-separated list of channel numbers to add, using the keyword 'all' will attempt to add all channels for the VM
set_channel_status [Fixed STRING]:
set_channel_status <vm_name> <list>|all enabled|disabled, enable or disable the communication channels in list(comma-seperated) for the specified VM, alternatively list can be replaced with keyword 'all'. Disabled channels will still receive packets on the host, however the commands they specify will be ignored. Set status to 'enabled' to begin processing requests again.
show_vm [Fixed STRING]: show_vm <vm_name>, prints the information on the
specified VM(s), the information lists the number of vCPUS, the pinning to pCPU(s) as a bit mask, along with any communication channels associated with each VM
show_cpu_freq_mask [Fixed STRING]: show_cpu_freq_mask <mask>, Get the current
frequency for each core specified in the mask
set_cpu_freq_mask [Fixed STRING]: set_cpu_freq <core_mask> <up|down|min|max>,
Set the current frequency for the cores specified in <core_mask> by scaling each up/down/min/max.
show_cpu_freq [Fixed STRING]: Get the current frequency for the specified core
set_cpu_freq [Fixed STRING]: set_cpu_freq <core_num> <up|down|min|max>,
Set the current frequency for the specified core by scaling up/down/min/max
quit [Fixed STRING]: close the application
--- examples/vm_power_manager/vm_power_cli.c | 669 ++++++++++++++++++++++++++++++ examples/vm_power_manager/vm_power_cli.h | 47 ++ 2 files changed, 716 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/vm_power_cli.c create mode 100644 examples/vm_power_manager/vm_power_cli.h
examples/vm_power_manager/vm_power_cli.c new file +669 -0
@@ -0,0 +1,669 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdlib.h> +#include <stdint.h> +#include <inttypes.h> +#include <stdio.h> +#include <string.h> +#include <termios.h> +#include <errno.h> + +#include <cmdline_rdline.h> +#include <cmdline_parse.h> +#include <cmdline_parse_string.h> +#include <cmdline_parse_num.h> +#include <cmdline_socket.h> +#include <cmdline.h> +#include <rte_config.h> + +#include "vm_power_cli.h" +#include "channel_manager.h" +#include "channel_monitor.h" +#include "power_manager.h" +#include "channel_commands.h" + +struct cmd_quit_result { +cmdline_fixed_string_t quit; +}; + +static void cmd_quit_parsed (__attribute__((unused))void *parsed_result, +struct cmdline *cl, + __attribute__((unused))void *data) +{ +channel_monitor_exit (); +channel_manager_exit (); +power_manager_exit (); +cmdline_quit (cl); +} + +cmdline_parse_token_string_t cmd_quit_quit = + TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit"); + +cmdline_parse_inst_t cmd_quit = { + .f = cmd_quit_parsed,/* function to call */ + .data =NULL ,/* 2nd arg of func */ + .help_str ="close the application" , + .tokens = {/* token list, NULL terminated */ + (void *)&cmd_quit_quit, +NULL , + }, +}; + +/* *** VM operations *** */ +struct cmd_show_vm_result { +cmdline_fixed_string_t show_vm; +cmdline_fixed_string_t vm_name; +}; + +static void +cmd_show_vm_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_show_vm_result *res = parsed_result; +struct vm_info info; +unsigned i; + +if (get_info_vm (res->vm_name, &info) !=0 ) +return ; +cmdline_printf (cl,"VM: '%s', status = " , info.name); +if (info.status ==CHANNEL_MGR_VM_ACTIVE ) +cmdline_printf (cl,"ACTIVE\n" ); +else +cmdline_printf (cl,"INACTIVE\n" ); +cmdline_printf (cl,"Channels %u\n" , info.num_channels); +for (i =0 ; i < info.num_channels; i++) { +cmdline_printf (cl," [%u]: %s, status = " , i, + info.channels[i].channel_path); +switch (info.channels[i].status) { +case CHANNEL_MGR_CHANNEL_CONNECTED : +cmdline_printf (cl,"CONNECTED\n" ); +break ; +case CHANNEL_MGR_CHANNEL_DISCONNECTED : +cmdline_printf (cl,"DISCONNECTED\n" ); +break ; +case CHANNEL_MGR_CHANNEL_DISABLED : +cmdline_printf (cl,"DISABLED\n" ); +break ; +case CHANNEL_MGR_CHANNEL_PROCESSING : +cmdline_printf (cl,"PROCESSING\n" ); +break ; +default : +cmdline_printf (cl,"UNKNOWN\n" ); +break ; + } + } +cmdline_printf (cl,"Virtual CPU(s): %u\n" , info.num_vcpus); +for (i =0 ; i < info.num_vcpus; i++) { +cmdline_printf (cl," [%u]: Physical CPU Mask 0x%" PRIx64"\n" , i, + info.pcpu_mask[i]); + } +} + + + +cmdline_parse_token_string_t cmd_vm_show = + TOKEN_STRING_INITIALIZER(struct cmd_show_vm_result, + show_vm,"show_vm" ); +cmdline_parse_token_string_t cmd_show_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_show_vm_result, + vm_name,NULL ); + +cmdline_parse_inst_t cmd_show_vm_set = { + .f = cmd_show_vm_parsed, + .data =NULL , + .help_str ="show_vm <vm_name>, prints the information on the " +"specified VM(s), the information lists the number of vCPUS, the " +"pinning to pCPU(s) as a bit mask, along with any communication " +"channels associated with each VM" , + .tokens = { + (void *)&cmd_vm_show, + (void *)&cmd_show_vm_name, +NULL , + }, +}; + +/* *** vCPU to pCPU mapping operations *** */ +struct cmd_set_pcpu_mask_result { +cmdline_fixed_string_t set_pcpu_mask; +cmdline_fixed_string_t vm_name; +uint8_t vcpu; +uint64_t core_mask; +}; + +static void +cmd_set_pcpu_mask_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_set_pcpu_mask_result *res = parsed_result; +if (set_pcpus_mask (res->vm_name, res->vcpu, res->core_mask) ==0 ) +cmdline_printf (cl,"Pinned vCPU(%" PRId8") to pCPU core " +"mask(0x%" PRIx64")\n" , res->vcpu, res->core_mask); +else +cmdline_printf (cl,"Unable to pin vCPU(%" PRId8") to pCPU core " +"mask(0x%" PRIx64")\n" , res->vcpu, res->core_mask); +} + +cmdline_parse_token_string_t cmd_set_pcpu_mask = + TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_mask_result, + set_pcpu_mask,"set_pcpu_mask" ); +cmdline_parse_token_string_t cmd_set_pcpu_mask_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_mask_result, + vm_name,NULL ); +cmdline_parse_token_num_t set_pcpu_mask_vcpu = + TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_mask_result, + vcpu,UINT8 ); +cmdline_parse_token_num_t set_pcpu_mask_core_mask = + TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_mask_result, + core_mask,UINT64 ); + + +cmdline_parse_inst_t cmd_set_pcpu_mask_set = { + .f = cmd_set_pcpu_mask_parsed, + .data =NULL , + .help_str ="set_pcpu_mask <vm_name> <vcpu> <pcpu>, Set the binding " +"of Virtual CPU on VM to the Physical CPU mask." , + .tokens = { + (void *)&cmd_set_pcpu_mask, + (void *)&cmd_set_pcpu_mask_vm_name, + (void *)&set_pcpu_mask_vcpu, + (void *)&set_pcpu_mask_core_mask, +NULL , + }, +}; + +struct cmd_set_pcpu_result { +cmdline_fixed_string_t set_pcpu; +cmdline_fixed_string_t vm_name; +uint8_t vcpu; +uint8_t core; +}; + +static void +cmd_set_pcpu_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_set_pcpu_result *res = parsed_result; +if (set_pcpu (res->vm_name, res->vcpu, res->core) ==0 ) +cmdline_printf (cl,"Pinned vCPU(%" PRId8") to pCPU core " +"%" PRId8")\n" , res->vcpu, res->core); +else +cmdline_printf (cl,"Unable to pin vCPU(%" PRId8") to pCPU core " +"%" PRId8")\n" , res->vcpu, res->core); +} + +cmdline_parse_token_string_t cmd_set_pcpu = + TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_result, + set_pcpu,"set_pcpu" ); +cmdline_parse_token_string_t cmd_set_pcpu_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_result, + vm_name,NULL ); +cmdline_parse_token_num_t set_pcpu_vcpu = + TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_result, + vcpu,UINT8 ); +cmdline_parse_token_num_t set_pcpu_core = + TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_result, + core,UINT64 ); + + +cmdline_parse_inst_t cmd_set_pcpu_set = { + .f = cmd_set_pcpu_parsed, + .data =NULL , + .help_str ="set_pcpu <vm_name> <vcpu> <pcpu>, Set the binding " +"of Virtual CPU on VM to the Physical CPU." , + .tokens = { + (void *)&cmd_set_pcpu, + (void *)&cmd_set_pcpu_vm_name, + (void *)&set_pcpu_vcpu, + (void *)&set_pcpu_core, +NULL , + }, +}; + +struct cmd_vm_op_result { +cmdline_fixed_string_t op_vm; +cmdline_fixed_string_t vm_name; +}; + +static void +cmd_vm_op_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_vm_op_result *res = parsed_result; + +if (!strcmp (res->op_vm,"add_vm" )) { +if (add_vm (res->vm_name) <0 ) +cmdline_printf (cl,"Unable to add VM '%s'\n" , res->vm_name); + }else if (remove_vm (res->vm_name) <0 ) +cmdline_printf (cl,"Unable to remove VM '%s'\n" , res->vm_name); +} + +cmdline_parse_token_string_t cmd_vm_op = + TOKEN_STRING_INITIALIZER(struct cmd_vm_op_result, + op_vm,"add_vm#rm_vm" ); +cmdline_parse_token_string_t cmd_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_vm_op_result, + vm_name,NULL ); + +cmdline_parse_inst_t cmd_vm_op_set = { + .f = cmd_vm_op_parsed, + .data =NULL , + .help_str ="add_vm|rm_vm <name>, add a VM for " +"subsequent operations with the CLI or remove a previously added " +"VM from the VM Power Manager" , + .tokens = { + (void *)&cmd_vm_op, + (void *)&cmd_vm_name, +NULL , + }, +}; + +/* *** VM channel operations *** */ +struct cmd_channels_op_result { +cmdline_fixed_string_t op; +cmdline_fixed_string_t vm_name; +cmdline_fixed_string_t channel_list; +}; +static void +cmd_channels_op_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +unsigned num_channels =0 , channel_num, i; +int channels_added; +unsigned channel_list[CHANNEL_CMDS_MAX_VM_CHANNELS ]; +char *token, *remaining, *tail_ptr; +struct cmd_channels_op_result *res = parsed_result; + +if (!strcmp (res->channel_list,"all" )) { + channels_added =add_all_channels (res->vm_name); +cmdline_printf (cl,"Added %d channels for VM '%s'\n" , + channels_added, res->vm_name); +return ; + } + + remaining = res->channel_list; +while (1 ) { +if (remaining ==NULL || remaining[0 ] =='\0' ) +break ; + + token =strsep (&remaining,"," ); +if (token ==NULL ) +break ; + errno =0 ; + channel_num = (unsigned )strtol (token, &tail_ptr,10 ); +if ((errno !=0 ) || (*tail_ptr !='\0' ) || tail_ptr ==NULL ) +break ; + +if (channel_num ==CHANNEL_CMDS_MAX_VM_CHANNELS ) { +cmdline_printf (cl,"Channel number '%u' exceeds the maximum number " +"of allowable channels(%u) for VM '%s'\n" , channel_num, +CHANNEL_CMDS_MAX_VM_CHANNELS , res->vm_name); +return ; + } + channel_list[num_channels++] = channel_num; + } +for (i =0 ; i < num_channels; i++) +cmdline_printf (cl,"[%u]: Adding channel %u\n" , i, channel_list[i]); + + channels_added =add_channels (res->vm_name, channel_list, + num_channels); +cmdline_printf (cl,"Enabled %d channels for '%s'\n" , channels_added, + res->vm_name); +} + +cmdline_parse_token_string_t cmd_channels_op = + TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result, + op,"add_channels" ); +cmdline_parse_token_string_t cmd_channels_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result, + vm_name,NULL ); +cmdline_parse_token_string_t cmd_channels_list = + TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result, + channel_list,NULL ); + +cmdline_parse_inst_t cmd_channels_op_set = { + .f = cmd_channels_op_parsed, + .data =NULL , + .help_str ="add_channels <vm_name> <list>|all, add " +"communication channels for the specified VM, the " +"virtio channels must be enabled in the VM " +"configuration(qemu/libvirt) and the associated VM must be active. " +"<list> is a comma-seperated list of channel numbers to add, using " +"the keyword 'all' will attempt to add all channels for the VM" , + .tokens = { + (void *)&cmd_channels_op, + (void *)&cmd_channels_vm_name, + (void *)&cmd_channels_list, +NULL , + }, +}; + +struct cmd_channels_status_op_result { +cmdline_fixed_string_t op; +cmdline_fixed_string_t vm_name; +cmdline_fixed_string_t channel_list; +cmdline_fixed_string_t status; +}; + +static void +cmd_channels_status_op_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +unsigned num_channels =0 , channel_num; +int changed; +unsigned channel_list[CHANNEL_CMDS_MAX_VM_CHANNELS ]; +char *token, *remaining, *tail_ptr; +struct cmd_channels_status_op_result *res = parsed_result; +enum channel_status status; + +if (!strcmp (res->status,"enabled" )) + status =CHANNEL_MGR_CHANNEL_CONNECTED ; +else + status =CHANNEL_MGR_CHANNEL_DISABLED ; + +if (!strcmp (res->channel_list,"all" )) { + changed =set_channel_status_all (res->vm_name, status); +cmdline_printf (cl,"Updated status of %d channels " +"for VM '%s'\n" , changed, res->vm_name); +return ; + } + remaining = res->channel_list; +while (1 ) { +if (remaining ==NULL || remaining[0 ] =='\0' ) +break ; + token =strsep (&remaining,"," ); +if (token ==NULL ) +break ; + errno =0 ; + channel_num = (unsigned )strtol (token, &tail_ptr,10 ); +if ((errno !=0 ) || (*tail_ptr !='\0' ) || tail_ptr ==NULL ) +break ; + +if (channel_num ==CHANNEL_CMDS_MAX_VM_CHANNELS ) { +cmdline_printf (cl,"%u exceeds the maximum number of allowable " +"channels(%u) for VM '%s'\n" , channel_num, +CHANNEL_CMDS_MAX_VM_CHANNELS , res->vm_name); +return ; + } + channel_list[num_channels++] = channel_num; + } + changed =set_channel_status (res->vm_name, channel_list, num_channels, + status); +cmdline_printf (cl,"Updated status of %d channels " +"for VM '%s'\n" , changed, res->vm_name); +} + +cmdline_parse_token_string_t cmd_channels_status_op = + TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result, + op,"set_channel_status" ); +cmdline_parse_token_string_t cmd_channels_status_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result, + vm_name,NULL ); +cmdline_parse_token_string_t cmd_channels_status_list = + TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result, + channel_list,NULL ); +cmdline_parse_token_string_t cmd_channels_status = + TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result, + status,"enabled#disabled" ); + +cmdline_parse_inst_t cmd_channels_status_op_set = { + .f = cmd_channels_status_op_parsed, + .data =NULL , + .help_str ="set_channel_status <vm_name> <list>|all enabled|disabled, " +" enable or disable the communication channels in " +"list(comma-seperated) for the specified VM, alternatively list can" +" be replaced with keyword 'all'. Disabled channels will still " +"receive packets on the host, however the commands they specify " +"will be ignored. Set status to 'enabled' to begin processing " +"requests again." , + .tokens = { + (void *)&cmd_channels_status_op, + (void *)&cmd_channels_status_vm_name, + (void *)&cmd_channels_status_list, + (void *)&cmd_channels_status, +NULL , + }, +}; + +/* *** CPU Frequency operations *** */ +struct cmd_show_cpu_freq_mask_result { +cmdline_fixed_string_t show_cpu_freq_mask; +uint64_t core_mask; +}; + +static void +cmd_show_cpu_freq_mask_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_show_cpu_freq_mask_result *res = parsed_result; +unsigned i; +uint64_t mask = res->core_mask; +uint32_t freq; +for (i =0 ; mask; mask &= ~(1ULL << i++)) { +if ((mask >> i) &1 ) { + freq =power_manager_get_current_frequency (i); +if (freq >0 ) +cmdline_printf (cl,"Core %u: %" PRId32"\n" , i, freq); + } + } +} + +cmdline_parse_token_string_t cmd_show_cpu_freq_mask = + TOKEN_STRING_INITIALIZER(struct cmd_show_cpu_freq_mask_result, + show_cpu_freq_mask,"show_cpu_freq_mask" ); +cmdline_parse_token_num_t cmd_show_cpu_freq_mask_core_mask = + TOKEN_NUM_INITIALIZER(struct cmd_show_cpu_freq_mask_result, + core_mask,UINT64 ); + +cmdline_parse_inst_t cmd_show_cpu_freq_mask_set = { + .f = cmd_show_cpu_freq_mask_parsed, + .data =NULL , + .help_str ="show_cpu_freq_mask <mask>, Get the current frequency for each " +"core specified in the mask" , + .tokens = { + (void *)&cmd_show_cpu_freq_mask, + (void *)&cmd_show_cpu_freq_mask_core_mask, +NULL , + }, +}; + +struct cmd_set_cpu_freq_mask_result { +cmdline_fixed_string_t set_cpu_freq_mask; +uint64_t core_mask; +cmdline_fixed_string_t cmd; +}; + +static void +cmd_set_cpu_freq_mask_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_set_cpu_freq_mask_result *res = parsed_result; +int ret =-1 ; + +if (!strcmp (res->cmd ,"up" )) + ret =power_manager_scale_mask_up (res->core_mask); +else if (!strcmp (res->cmd ,"down" )) + ret =power_manager_scale_mask_down (res->core_mask); +else if (!strcmp (res->cmd ,"min" )) + ret =power_manager_scale_mask_min (res->core_mask); +else if (!strcmp (res->cmd ,"max" )) + ret =power_manager_scale_mask_max (res->core_mask); +if (ret <0 ) { +cmdline_printf (cl,"Error scaling core_mask(0x%" PRIx64") '%s' , not " +"all cores specified have been scaled\n" , + res->core_mask, res->cmd); + }; +} + +cmdline_parse_token_string_t cmd_set_cpu_freq_mask = + TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_mask_result, + set_cpu_freq_mask,"set_cpu_freq_mask" ); +cmdline_parse_token_num_t cmd_set_cpu_freq_mask_core_mask = + TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_mask_result, + core_mask,UINT64 ); +cmdline_parse_token_string_t cmd_set_cpu_freq_mask_result = + TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_mask_result, + cmd,"up#down#min#max" ); + +cmdline_parse_inst_t cmd_set_cpu_freq_mask_set = { + .f = cmd_set_cpu_freq_mask_parsed, + .data =NULL , + .help_str ="set_cpu_freq <core_mask> <up|down|min|max>, Set the current " +"frequency for the cores specified in <core_mask> by scaling " +"each up/down/min/max." , + .tokens = { + (void *)&cmd_set_cpu_freq_mask, + (void *)&cmd_set_cpu_freq_mask_core_mask, + (void *)&cmd_set_cpu_freq_mask_result, +NULL , + }, +}; + + + +struct cmd_show_cpu_freq_result { +cmdline_fixed_string_t show_cpu_freq; +uint8_t core_num; +}; + +static void +cmd_show_cpu_freq_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_show_cpu_freq_result *res = parsed_result; +uint32_t curr_freq =power_manager_get_current_frequency (res->core_num); +if (curr_freq ==0 ) { +cmdline_printf (cl,"Unable to get frequency for core %u\n" , + res->core_num); +return ; + } +cmdline_printf (cl,"Core %u frequency: %" PRId32"\n" , res->core_num, + curr_freq); +} + +cmdline_parse_token_string_t cmd_show_cpu_freq = + TOKEN_STRING_INITIALIZER(struct cmd_show_cpu_freq_result, + show_cpu_freq,"show_cpu_freq" ); + +cmdline_parse_token_num_t cmd_show_cpu_freq_core_num = + TOKEN_NUM_INITIALIZER(struct cmd_show_cpu_freq_result, + core_num,UINT8 ); + +cmdline_parse_inst_t cmd_show_cpu_freq_set = { + .f = cmd_show_cpu_freq_parsed, + .data =NULL , + .help_str ="Get the current frequency for the specified core" , + .tokens = { + (void *)&cmd_show_cpu_freq, + (void *)&cmd_show_cpu_freq_core_num, +NULL , + }, +}; + +struct cmd_set_cpu_freq_result { +cmdline_fixed_string_t set_cpu_freq; +uint8_t core_num; +cmdline_fixed_string_t cmd; +}; + +static void +cmd_set_cpu_freq_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +int ret =-1 ; +struct cmd_set_cpu_freq_result *res = parsed_result; + +if (!strcmp (res->cmd ,"up" )) + ret =power_manager_scale_core_up (res->core_num); +else if (!strcmp (res->cmd ,"down" )) + ret =power_manager_scale_core_down (res->core_num); +else if (!strcmp (res->cmd ,"min" )) + ret =power_manager_scale_core_min (res->core_num); +else if (!strcmp (res->cmd ,"max" )) + ret =power_manager_scale_core_max (res->core_num); +if (ret <0 ) { +cmdline_printf (cl,"Error scaling core(%u) '%s'\n" , res->core_num, + res->cmd); + } +} + +cmdline_parse_token_string_t cmd_set_cpu_freq = + TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result, + set_cpu_freq,"set_cpu_freq" ); +cmdline_parse_token_string_t cmd_set_cpu_freq_core_num = + TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_result, + core_num,UINT8 ); +cmdline_parse_token_string_t cmd_set_cpu_freq_cmd_cmd = + TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result, + cmd,"up#down#min#max" ); + +cmdline_parse_inst_t cmd_set_cpu_freq_set = { + .f = cmd_set_cpu_freq_parsed, + .data =NULL , + .help_str ="set_cpu_freq <core_num> <up|down|min|max>, Set the current " +"frequency for the specified core by scaling up/down/min/max" , + .tokens = { + (void *)&cmd_set_cpu_freq, + (void *)&cmd_set_cpu_freq_core_num, + (void *)&cmd_set_cpu_freq_cmd_cmd, +NULL , + }, +}; + +cmdline_parse_ctx_t main_ctx[] = { + (cmdline_parse_inst_t *)&cmd_quit, + (cmdline_parse_inst_t *)&cmd_vm_op_set, + (cmdline_parse_inst_t *)&cmd_channels_op_set, + (cmdline_parse_inst_t *)&cmd_channels_status_op_set, + (cmdline_parse_inst_t *)&cmd_show_vm_set, + (cmdline_parse_inst_t *)&cmd_show_cpu_freq_mask_set, + (cmdline_parse_inst_t *)&cmd_set_cpu_freq_mask_set, + (cmdline_parse_inst_t *)&cmd_show_cpu_freq_set, + (cmdline_parse_inst_t *)&cmd_set_cpu_freq_set, + (cmdline_parse_inst_t *)&cmd_set_pcpu_mask_set, + (cmdline_parse_inst_t *)&cmd_set_pcpu_set, +NULL , +}; + +void +run_cli (__attribute__((unused))void *arg) +{ +struct cmdline *cl; + + cl =cmdline_stdin_new (main_ctx,"vmpower> " ); +if (cl ==NULL ) +return ; + +cmdline_interact (cl); +cmdline_stdin_exit (cl); +}
examples/vm_power_manager/vm_power_cli.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v5 3/10 CPU Frequency Power Management(Host).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
A wrapper around librte_power(using ACPI cpufreq), providing locking around the non-threadsafe library, allowing for frequency changes based on core masks and core numbers from both the CLI thread and epoll monitor thread.
--- examples/vm_power_manager/power_manager.c | 244 +++++++++++++++++++++++++++++ examples/vm_power_manager/power_manager.h | 188 ++++++++++++++++++++++ 2 files changed, 432 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/power_manager.c create mode 100644 examples/vm_power_manager/power_manager.h
examples/vm_power_manager/power_manager.c new file +244 -0
@@ -0,0 +1,244 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <inttypes.h> +#include <sys/un.h> +#include <fcntl.h> +#include <unistd.h> +#include <dirent.h> +#include <errno.h> + +#include <sys/types.h> + +#include <rte_config.h> +#include <rte_log.h> +#include <rte_power.h> +#include <rte_spinlock.h> + +#include "power_manager.h" + +#define RTE_LOGTYPE_POWER_MANAGER RTE_LOGTYPE_USER1 + +#define POWER_SCALE_CORE (DIRECTION , core_num , ret) do { \ + if (core_num >= POWER_MGR_MAX_CPUS) \ + return -1; \ + if (!(global_enabled_cpus & (1ULL << core_num))) \ + return -1; \ + rte_spinlock_lock(&global_core_freq_info[core_num].power_sl); \ + ret = rte_power_freq_##DIRECTION(core_num); \ + rte_spinlock_unlock(&global_core_freq_info[core_num].power_sl); \ +} while (0) + +#define POWER_SCALE_MASK (DIRECTION , core_mask, ret) do { \ + int i; \ + for (i = 0; core_mask; core_mask &= ~(1 << i++)) { \ + if ((core_mask >> i) & 1) { \ + if (!(global_enabled_cpus & (1ULL << i))) \ + continue; \ + rte_spinlock_lock(&global_core_freq_info[i].power_sl); \ + if (rte_power_freq_##DIRECTION(i) != 1) \ + ret = -1; \ + rte_spinlock_unlock(&global_core_freq_info[i].power_sl); \ + } \ + } \ +} while (0) + +struct freq_info { +rte_spinlock_t power_sl; +uint32_t freqs[RTE_MAX_LCORE_FREQS ]; +unsigned num_freqs; +} __rte_cache_aligned; + +static struct freq_info global_core_freq_info[POWER_MGR_MAX_CPUS ]; + +static uint64_t global_enabled_cpus; + +#define SYSFS_CPU_PATH "/sys/devices/system/cpu/cpu%u/topology/core_id" + +static unsigned +set_host_cpus_mask (void ) +{ +char path[PATH_MAX ]; +unsigned i; +unsigned num_cpus =0 ; +for (i =0 ; i <POWER_MGR_MAX_CPUS ; i++) { +snprintf (path,sizeof (path),SYSFS_CPU_PATH , i); +if (access (path,F_OK ) ==0 ) { + global_enabled_cpus |=1ULL << i; + num_cpus++; + }else +return num_cpus; + } +return num_cpus; +} + +int +power_manager_init (void ) +{ +unsigned i, num_cpus; +uint64_t cpu_mask; +int ret =0 ; + + num_cpus =set_host_cpus_mask (); +if (num_cpus ==0 ) { +RTE_LOG (ERR ,POWER_MANAGER ,"Unable to detected host CPUs, please " +"ensure that sufficient privileges exist to inspect sysfs\n" ); +return -1 ; + } +rte_power_set_env (PM_ENV_ACPI_CPUFREQ ); + cpu_mask = global_enabled_cpus; +for (i =0 ; cpu_mask; cpu_mask &= ~(1 << i++)) { +if (rte_power_init (i) <0 ||rte_power_freqs (i, + global_core_freq_info[i].freqs, +RTE_MAX_LCORE_FREQS ) ==0 ) { +RTE_LOG (ERR ,POWER_MANAGER ,"Unable to initialize power manager " +"for core %u\n" , i); + global_enabled_cpus &= ~(1 << i); + num_cpus--; + ret =-1 ; + } +rte_spinlock_init (&global_core_freq_info[i].power_sl); + } +RTE_LOG (INFO ,POWER_MANAGER ,"Detected %u host CPUs , enabled core mask:" +" 0x%" PRIx64"\n" , num_cpus, global_enabled_cpus); +return ret; + +} + +uint32_t +power_manager_get_current_frequency (unsigned core_num) +{ +uint32_t freq, index; + +if (core_num >=POWER_MGR_MAX_CPUS ) { +RTE_LOG (ERR ,POWER_MANAGER ,"Core(%u) is out of range 0...%d\n" , + core_num,POWER_MGR_MAX_CPUS -1 ); +return -1 ; + } +if (!(global_enabled_cpus & (1ULL << core_num))) +return 0 ; + +rte_spinlock_lock (&global_core_freq_info[core_num].power_sl); + index =rte_power_get_freq (core_num); +rte_spinlock_unlock (&global_core_freq_info[core_num].power_sl); +if (index >=POWER_MGR_MAX_CPUS ) + freq =0 ; +else + freq = global_core_freq_info[core_num].freqs[index]; + +return freq; +} + +int +power_manager_exit (void ) +{ +unsigned int i; +int ret =0 ; + +for (i =0 ; global_enabled_cpus; global_enabled_cpus &= ~(1 << i++)) { +if (rte_power_exit (i) <0 ) { +RTE_LOG (ERR ,POWER_MANAGER ,"Unable to shutdown power manager " +"for core %u\n" , i); + ret =-1 ; + } + } + global_enabled_cpus =0 ; +return ret; +} + +int +power_manager_scale_mask_up (uint64_t core_mask) +{ +int ret =0 ; +POWER_SCALE_MASK (up, core_mask, ret); +return ret; +} + +int +power_manager_scale_mask_down (uint64_t core_mask) +{ +int ret =0 ; +POWER_SCALE_MASK (down, core_mask, ret); +return ret; +} + +int +power_manager_scale_mask_min (uint64_t core_mask) +{ +int ret =0 ; +POWER_SCALE_MASK (min, core_mask, ret); +return ret; +} + +int +power_manager_scale_mask_max (uint64_t core_mask) +{ +int ret =0 ; +POWER_SCALE_MASK (max, core_mask, ret); +return ret; +} + +int +power_manager_scale_core_up (unsigned core_num) +{ +int ret =0 ; +POWER_SCALE_CORE (up, core_num, ret); +return ret; +} + +int +power_manager_scale_core_down (unsigned core_num) +{ +int ret =0 ; +POWER_SCALE_CORE (down, core_num, ret); +return ret; +} + +int +power_manager_scale_core_min (unsigned core_num) +{ +int ret =0 ; +POWER_SCALE_CORE (min, core_num, ret); +return ret; +} + +int +power_manager_scale_core_max (unsigned core_num) +{ +int ret =0 ; +POWER_SCALE_CORE (max, core_num, ret); +return ret; +}
examples/vm_power_manager/power_manager.h new file +188 -0
188 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v5 4/10 VM Power Management application and Makefile.
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
For launching CLI thread and Monitor thread and initialising resources.
Requires a minimum of two lcores to run, additional cores specified by eal core mask are not used.
--- examples/vm_power_manager/Makefile | 57 +++++++++++++++++ examples/vm_power_manager/main.c | 117 ++++++++++++++++++++++++++++++++++++ examples/vm_power_manager/main.h | 52 ++++++++++++++++ 3 files changed, 226 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/Makefile create mode 100644 examples/vm_power_manager/main.c create mode 100644 examples/vm_power_manager/main.h
examples/vm_power_manager/Makefile new file +57 -0
@@ -0,0 +1,57 @@ +# BSD LICENSE +# +# Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +ifeq ($(RTE_SDK),) +$(error "Please define RTE_SDK environment variable") +endif + +# Default target, can be overriden by command line or environment +RTE_TARGET ?= x86_64-native-linuxapp-gcc + +include $(RTE_SDK)/mk/rte.vars.mk + +# binary name +APP = vm_power_mgr + +# all source are stored in SRCS-y +SRCS-y := main.c vm_power_cli.c power_manager.c channel_manager.c +SRCS-y += channel_monitor.c + +CFLAGS += -O3 -lvirt -I$(RTE_SDK)/lib/librte_power/ +CFLAGS += $(WERROR_FLAGS) + +# workaround for a gcc bug with noreturn attribute +# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12603 +ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y) +CFLAGS_main.o += -Wno-return-type +endif + +include $(RTE_SDK)/mk/rte.extapp.mk
examples/vm_power_manager/main.c new file +117 -0
117 lines not on this page · raw message
examples/vm_power_manager/main.h new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v5 5/10 VM Power Management CLI(Guest).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Provides a small sample application(guest_vm_power_mgr) to run on a VM. The application is run by providing a core mask(-c) and number of memory channels(-n). The core mask corresponds to the number of lcore channels to attempt to open. A maximum of 64 channels per VM is allowed. The channels must be monitored by the host.
After successful initialisation a CPU frequency command can be sent to the host using:set_cpu_freq <lcore_num> <up|down|min|max>.
--- examples/vm_power_manager/guest_cli/Makefile | 56 +++++++ examples/vm_power_manager/guest_cli/main.c | 87 +++++++++++ examples/vm_power_manager/guest_cli/main.h | 52 +++++++ .../guest_cli/vm_power_cli_guest.c | 155 ++++++++++++++++++++ .../guest_cli/vm_power_cli_guest.h | 55 +++++++ 5 files changed, 405 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/guest_cli/Makefile create mode 100644 examples/vm_power_manager/guest_cli/main.c create mode 100644 examples/vm_power_manager/guest_cli/main.h create mode 100644 examples/vm_power_manager/guest_cli/vm_power_cli_guest.c create mode 100644 examples/vm_power_manager/guest_cli/vm_power_cli_guest.h
examples/vm_power_manager/guest_cli/Makefile new file +56 -0
@@ -0,0 +1,56 @@ +# BSD LICENSE +# +# Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +ifeq ($(RTE_SDK),) +$(error "Please define RTE_SDK environment variable") +endif + +# Default target, can be overriden by command line or environment +RTE_TARGET ?= x86_64-native-linuxapp-gcc + +include $(RTE_SDK)/mk/rte.vars.mk + +# binary name +APP = guest_vm_power_mgr + +# all source are stored in SRCS-y +SRCS-y := main.c vm_power_cli_guest.c + +CFLAGS += -O3 -I$(RTE_SDK)/lib/librte_power/ +CFLAGS += $(WERROR_FLAGS) + +# workaround for a gcc bug with noreturn attribute +# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12603 +ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y) +CFLAGS_main.o += -Wno-return-type +endif + +include $(RTE_SDK)/mk/rte.extapp.mk
examples/vm_power_manager/guest_cli/main.c new file +87 -0
87 lines not on this page · raw message
examples/vm_power_manager/guest_cli/main.h new file +52 -0
52 lines not on this page · raw message
examples/vm_power_manager/guest_cli/vm_power_cli_guest.c new file +155 -0
155 lines not on this page · raw message
examples/vm_power_manager/guest_cli/vm_power_cli_guest.h new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v5 6/10 VM communication channels for VM Power Management(Guest).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Allows for the opening of Virtio-Serial devices on a VM, where a DPDK application can send packets to the host based monitor. The packet formatted is specified in channel_commands.h
Each device appears as a serial device in path /dev/virtio-ports/virtio.serial.port.<agent_type>.<lcore_num> where each lcore in a DPDK application has exclusive to a device/channel.
Each channel is opened in non-blocking mode, after a successful open a test packet is send to the host to ensure the host side is monitoring.
--- lib/librte_power/guest_channel.c | 162 ++++++++++++++++++++++++++++++++++++++ lib/librte_power/guest_channel.h | 89 +++++++++++++++++++++ 2 files changed, 251 insertions(+), 0 deletions(-) create mode 100644 lib/librte_power/guest_channel.c create mode 100644 lib/librte_power/guest_channel.h
lib/librte_power/guest_channel.c new file +162 -0
@@ -0,0 +1,162 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> +#include <limits.h> +#include <fcntl.h> +#include <string.h> +#include <errno.h> + + +#include <rte_log.h> +#include <rte_config.h> + +#include "guest_channel.h" +#include "channel_commands.h" + +#define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1 + +static int global_fds[RTE_MAX_LCORE ]; + +int +guest_channel_host_connect (const char *path,unsigned lcore_id) +{ +int flags, ret; +struct channel_packet pkt; +char fd_path[PATH_MAX ]; +int fd =-1 ; + +if (lcore_id >=RTE_MAX_LCORE ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel(%u) is out of range 0...%d\n" , + lcore_id,RTE_MAX_LCORE -1 ); +return -1 ; + } +/* check if path is already open */ +if (global_fds[lcore_id] !=0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel(%u) is already open with fd %d\n" , + lcore_id, global_fds[lcore_id]); +return -1 ; + } + +snprintf (fd_path,PATH_MAX ,"%s.%u" , path, lcore_id); +RTE_LOG (INFO ,GUEST_CHANNEL ,"Opening channel '%s' for lcore %u\n" , + fd_path, lcore_id); + fd =open (fd_path,O_RDWR ); +if (fd <0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Unable to to connect to '%s' with error " +"%s\n" , fd_path,strerror (errno)); +return -1 ; + } + + flags =fcntl (fd,F_GETFL ,0 ); +if (flags <0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Failed on fcntl get flags for file %s\n" , + fd_path); + goto error; + } + + flags |=O_NONBLOCK ; +if (fcntl (fd,F_SETFL , flags) <0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Failed on setting non-blocking mode for " +"file %s" , fd_path); + goto error; + } +/* QEMU needs a delay after connection */ +sleep (1 ); + +/* Send a test packet, this command is ignored by the host, but a successful +* send indicates that the host endpoint is monitoring. +*/ + pkt.command =CPU_POWER_CONNECT ; + global_fds[lcore_id] = fd; + ret =guest_channel_send_msg (&pkt, lcore_id); +if (ret !=0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Error on channel '%s' communications " +"test: %s\n" , fd_path,strerror (ret)); + goto error; + } +RTE_LOG (INFO ,GUEST_CHANNEL ,"Channel '%s' is now connected\n" , fd_path); +return 0 ; +error: +close (fd); + global_fds[lcore_id] =0 ; +return -1 ; +} + +int +guest_channel_send_msg (struct channel_packet *pkt,unsigned lcore_id) +{ +int ret, buffer_len =sizeof (*pkt); +void *buffer = pkt; + +if (lcore_id >=RTE_MAX_LCORE ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel(%u) is out of range 0...%d\n" , + lcore_id,RTE_MAX_LCORE -1 ); +return -1 ; + } + +if (global_fds[lcore_id] ==0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel is not connected\n" ); +return -1 ; + } +while (buffer_len >0 ) { + ret =write (global_fds[lcore_id], buffer, buffer_len); +if (ret == buffer_len) +return 0 ; +if (ret ==-1 ) { +if (errno ==EINTR ) +continue ; +return errno; + } + buffer = (char *)buffer + ret; + buffer_len -= ret; + } +return 0 ; +} + +void +guest_channel_host_disconnect (unsigned lcore_id) +{ +if (lcore_id >=RTE_MAX_LCORE ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel(%u) is out of range 0...%d\n" , + lcore_id,RTE_MAX_LCORE -1 ); +return ; + } +if (global_fds[lcore_id] ==0 ) +return ; +close (global_fds[lcore_id]); + global_fds[lcore_id] =0 ; +}
lib/librte_power/guest_channel.h new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v5 7/10 librte_power common interface for Guest and Host
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Moved the current librte_power implementation to rte_power_acpi_cpufreq, with renaming of functions only.
Added rte_power_kvm_vm implmentation to support Power Management from a VM.
librte_power now hides the implementation based on the environment used. A new call rte_power_set_env() can explicidly set the environment, if not called then auto-detection takes place.
rte_power_kvm_vm is subset of the librte_power APIs, the following is supported:
rte_power_init(unsigned lcore_id)
rte_power_exit(unsigned lcore_id)
rte_power_freq_up(unsigned lcore_id)
rte_power_freq_down(unsigned lcore_id)
rte_power_freq_min(unsigned lcore_id)
rte_power_freq_max(unsigned lcore_id)
The other unsupported APIs return -ENOTSUP
--- lib/librte_power/rte_power.c | 540 ++++------------------------- lib/librte_power/rte_power.h | 120 +++++-- lib/librte_power/rte_power_acpi_cpufreq.c | 545 +++++++++++++++++++++++++++++ lib/librte_power/rte_power_acpi_cpufreq.h | 192 ++++++++++ lib/librte_power/rte_power_common.h | 39 ++ lib/librte_power/rte_power_kvm_vm.c | 135 +++++++ lib/librte_power/rte_power_kvm_vm.h | 179 ++++++++++ 7 files changed, 1248 insertions(+), 502 deletions(-) create mode 100644 lib/librte_power/rte_power_acpi_cpufreq.c create mode 100644 lib/librte_power/rte_power_acpi_cpufreq.h create mode 100644 lib/librte_power/rte_power_common.h create mode 100644 lib/librte_power/rte_power_kvm_vm.c create mode 100644 lib/librte_power/rte_power_kvm_vm.h
lib/librte_power/rte_power.c +69 -471
@@ -31,515 +31,113 @@* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <stdio.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <signal.h> -#include <limits.h> - -#include <rte_memcpy.h> #include <rte_atomic.h> #include "rte_power.h" +#include "rte_power_acpi_cpufreq.h" +#include "rte_power_kvm_vm.h" +#include "rte_power_common.h" -#ifdef RTE_LIBRTE_POWER_DEBUG -#define POWER_DEBUG_TRACE(fmt, args...) do { \ - RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \ - } while (0) -#else -#define POWER_DEBUG_TRACE(fmt, args...) -#endif - -#define FOPEN_OR_ERR_RET (f, retval) do { \ - if ((f) == NULL) { \ - RTE_LOG(ERR, POWER, "File not openned\n"); \ - return (retval); \ - } \ -} while(0) - -#define FOPS_OR_NULL_GOTO (ret, label) do { \ - if ((ret) == NULL) { \ - RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \ - goto label; \ - } \ -} while(0) - -#define FOPS_OR_ERR_GOTO (ret, label) do { \ - if ((ret) < 0) { \ - RTE_LOG(ERR, POWER, "File operations failed\n"); \ - goto label; \ - } \ -} while(0) - -#define STR_SIZE 1024 -#define POWER_CONVERT_TO_DECIMAL 10 +enum power_management_env global_default_env =PM_ENV_NOT_SET ; -#define POWER_GOVERNOR_USERSPACE "userspace" -#define POWER_SYSFILE_GOVERNOR \ - "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor" -#define POWER_SYSFILE_AVAIL_FREQ \ - "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies" -#define POWER_SYSFILE_SETSPEED \ - "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed" +volatile uint32_t global_env_cfg_status =0 ; -enum power_state { -POWER_IDLE =0 , -POWER_ONGOING , -POWER_USED , -POWER_UNKNOWN -}; +/* function pointers */ +rte_power_freqs_t rte_power_freqs =NULL ; +rte_power_get_freq_t rte_power_get_freq =NULL ; +rte_power_set_freq_t rte_power_set_freq =NULL ; +rte_power_freq_change_t rte_power_freq_up =NULL ; +rte_power_freq_change_t rte_power_freq_down =NULL ; +rte_power_freq_change_t rte_power_freq_max =NULL ; +rte_power_freq_change_t rte_power_freq_min =NULL ; -/** -* Power info per lcore. -*/ -struct rte_power_info { -unsigned lcore_id;/**< Logical core id */ -uint32_t freqs[RTE_MAX_LCORE_FREQS ];/**< Frequency array */ -uint32_t nb_freqs;/**< number of available freqs */ -FILE *f;/**< FD of scaling_setspeed */ -char governor_ori[32 ];/**< Original governor name */ -uint32_t curr_idx;/**< Freq index in freqs array */ -volatile uint32_t state;/**< Power in use state */ -} __rte_cache_aligned; - -static struct rte_power_info lcore_power_info[RTE_MAX_LCORE ]; - -/** -* It is to set specific freq for specific logical core, according to the index -* of supported frequencies. -*/ -static int -set_freq_internal (struct rte_power_info *pi,uint32_t idx) +int +rte_power_set_env (enum power_management_env env) { -if (idx >=RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) { -RTE_LOG (ERR ,POWER ,"Invalid frequency index %u, which " -"should be less than %u\n" , idx, pi->nb_freqs); -return -1 ; - } - -/* Check if it is the same as current */ -if (idx == pi->curr_idx) +if (rte_atomic32_cmpset (&global_env_cfg_status,0 ,1 ) ==0 ) {return 0 ; - -POWER_DEBUG_TRACE ("Freqency[%u] %u to be set for lcore %u\n" , - idx, pi->freqs[idx], pi->lcore_id); -if (fseek (pi->f,0 ,SEEK_SET ) <0 ) { -RTE_LOG (ERR ,POWER ,"Fail to set file position indicator to 0 " -"for setting frequency for lcore %u\n" , pi->lcore_id); -return -1 ; } -if (fprintf (pi->f,"%u" , pi->freqs[idx]) <0 ) { -RTE_LOG (ERR ,POWER ,"Fail to write new frequency for " -"lcore %u\n" , pi->lcore_id); +if (env ==PM_ENV_ACPI_CPUFREQ ) { + rte_power_freqs = rte_power_acpi_cpufreq_freqs; + rte_power_get_freq = rte_power_acpi_cpufreq_get_freq; + rte_power_set_freq = rte_power_acpi_cpufreq_set_freq; + rte_power_freq_up = rte_power_acpi_cpufreq_freq_up; + rte_power_freq_down = rte_power_acpi_cpufreq_freq_down; + rte_power_freq_min = rte_power_acpi_cpufreq_freq_min; + rte_power_freq_max = rte_power_acpi_cpufreq_freq_max; + }else if (env ==PM_ENV_KVM_VM ) { + rte_power_freqs = rte_power_kvm_vm_freqs; + rte_power_get_freq = rte_power_kvm_vm_get_freq; + rte_power_set_freq = rte_power_kvm_vm_set_freq; + rte_power_freq_up = rte_power_kvm_vm_freq_up; + rte_power_freq_down = rte_power_kvm_vm_freq_down; + rte_power_freq_min = rte_power_kvm_vm_freq_min; + rte_power_freq_max = rte_power_kvm_vm_freq_max; + }else { +RTE_LOG (ERR ,POWER ,"Invalid Power Management Environment(%d) set\n" , + env); +rte_power_unset_env ();return -1 ; } -fflush (pi->f); - pi->curr_idx = idx; - -return 1 ; -} - -/** -* It is to check the current scaling governor by reading sys file, and then -* set it into 'userspace' if it is not by writing the sys file. The original -* governor will be saved for rolling back. -*/ -static int -power_set_governor_userspace (struct rte_power_info *pi) -{ -FILE *f; -int ret =-1 ; -char buf[BUFSIZ ]; -char fullpath[PATH_MAX ]; -char *s; -int val; - -snprintf (fullpath,sizeof (fullpath),POWER_SYSFILE_GOVERNOR , - pi->lcore_id); - f =fopen (fullpath,"rw+" ); -FOPEN_OR_ERR_RET (f, ret); - - s =fgets (buf,sizeof (buf), f); -FOPS_OR_NULL_GOTO (s, out); - -/* Check if current governor is userspace */ -if (strncmp (buf,POWER_GOVERNOR_USERSPACE , -sizeof (POWER_GOVERNOR_USERSPACE )) ==0 ) { - ret =0 ; -POWER_DEBUG_TRACE ("Power management governor of lcore %u is " -"already userspace\n" , pi->lcore_id); - goto out; - } -/* Save the original governor */ -snprintf (pi->governor_ori,sizeof (pi->governor_ori),"%s" , buf); - -/* Write 'userspace' to the governor */ - val =fseek (f,0 ,SEEK_SET ); -FOPS_OR_ERR_GOTO (val, out); - - val =fputs (POWER_GOVERNOR_USERSPACE , f); -FOPS_OR_ERR_GOTO (val, out); - - ret =0 ; -RTE_LOG (INFO ,POWER ,"Power management governor of lcore %u has been " -"set to user space successfully\n" , pi->lcore_id); -out: -fclose (f); + global_default_env = env; +return 0 ; -return ret; } -/** -* It is to get the available frequencies of the specific lcore by reading the -* sys file. -*/ -static int -power_get_available_freqs (struct rte_power_info *pi) +void +rte_power_unset_env (void ) { -FILE *f; -int ret =-1 , i, count; -char *p; -char buf[BUFSIZ ]; -char fullpath[PATH_MAX ]; -char *freqs[RTE_MAX_LCORE_FREQS ]; -char *s; - -snprintf (fullpath,sizeof (fullpath),POWER_SYSFILE_AVAIL_FREQ , - pi->lcore_id); - f =fopen (fullpath,"r" ); -FOPEN_OR_ERR_RET (f, ret); - - s =fgets (buf,sizeof (buf), f); -FOPS_OR_NULL_GOTO (s, out); - -/* Strip the line break if there is */ - p =strchr (buf,'\n' ); -if (p !=NULL ) - *p =0 ; - -/* Split string into at most RTE_MAX_LCORE_FREQS frequencies */ - count =rte_strsplit (buf,sizeof (buf), freqs, -RTE_MAX_LCORE_FREQS ,' ' ); -if (count <=0 ) { -RTE_LOG (ERR ,POWER ,"No available frequency in " -"" POWER_SYSFILE_AVAIL_FREQ "\n" , pi->lcore_id); - goto out; - } -if (count >=RTE_MAX_LCORE_FREQS ) { -RTE_LOG (ERR ,POWER ,"Too many available frequencies : %d\n" , - count); - goto out; - } - -/* Store the available frequncies into power context */ -for (i =0 , pi->nb_freqs =0 ; i < count; i++) { -POWER_DEBUG_TRACE ("Lcore %u frequency[%d]: %s\n" , pi->lcore_id, - i, freqs[i]); - pi->freqs[pi->nb_freqs++] =strtoul (freqs[i], &p, -POWER_CONVERT_TO_DECIMAL ); - } - - ret =0 ; -POWER_DEBUG_TRACE ("%d frequencie(s) of lcore %u are available\n" , - count, pi->lcore_id); -out: -fclose (f); - -return ret; +if (rte_atomic32_cmpset (&global_env_cfg_status,1 ,0 ) !=0 ) + global_default_env =PM_ENV_NOT_SET ; } -/** -* It is to fopen the sys file for the future setting the lcore frequency. -*/ -static int -power_init_for_setting_freq (struct rte_power_info *pi) -{ -FILE *f; -char fullpath[PATH_MAX ]; -char buf[BUFSIZ ]; -uint32_t i, freq; -char *s; - -snprintf (fullpath,sizeof (fullpath),POWER_SYSFILE_SETSPEED , - pi->lcore_id); - f =fopen (fullpath,"rw+" ); -FOPEN_OR_ERR_RET (f,-1 ); - - s =fgets (buf,sizeof (buf), f); -FOPS_OR_NULL_GOTO (s, out); - - freq =strtoul (buf,NULL ,POWER_CONVERT_TO_DECIMAL ); -for (i =0 ; i < pi->nb_freqs; i++) { -if (freq == pi->freqs[i]) { - pi->curr_idx = i; - pi->f = f; -return 0 ; - } - } - -out: -fclose (f); - -return -1 ; +enum power_management_env +rte_power_get_env (void ) { +return global_default_env; }int rte_power_init (unsigned lcore_id) { -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Lcore id %u can not exceeds %u\n" , - lcore_id,RTE_MAX_LCORE -1U ); -return -1 ; - } - - pi = &lcore_power_info[lcore_id]; -if (rte_atomic32_cmpset (&(pi->state),POWER_IDLE ,POWER_ONGOING ) - ==0 ) { -RTE_LOG (INFO ,POWER ,"Power management of lcore %u is " -"in use\n" , lcore_id); -return -1 ; - } - - pi->lcore_id = lcore_id; -/* Check and set the governor */ -if (power_set_governor_userspace (pi) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot set governor of lcore %u to " -"userspace\n" , lcore_id); - goto fail; - } +int ret =-1 ; -/* Get the available frequencies */ -if (power_get_available_freqs (pi) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot get available frequencies of " -"lcore %u\n" , lcore_id); - goto fail; +if (global_default_env ==PM_ENV_ACPI_CPUFREQ ) { +return rte_power_acpi_cpufreq_init (lcore_id); } - -/* Init for setting lcore frequency */ -if (power_init_for_setting_freq (pi) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot init for setting frequency for " -"lcore %u\n" , lcore_id); - goto fail; +if (global_default_env ==PM_ENV_KVM_VM ) { +return rte_power_kvm_vm_init (lcore_id); } - -/* Set freq to max by default */ -if (rte_power_freq_max (lcore_id) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot set frequency of lcore %u " -"to max\n" , lcore_id); - goto fail; +/* Auto detect Environment */ +RTE_LOG (INFO ,POWER ,"Attempting to initialise ACPI cpufreq power " +"management...\n" ); + ret =rte_power_acpi_cpufreq_init (lcore_id); +if (ret ==0 ) { +rte_power_set_env (PM_ENV_ACPI_CPUFREQ ); + goto out; } -RTE_LOG (INFO ,POWER ,"Initialized successfully for lcore %u " -"power manamgement\n" , lcore_id); -rte_atomic32_cmpset (&(pi->state),POWER_ONGOING ,POWER_USED ); - -return 0 ; - -fail: -rte_atomic32_cmpset (&(pi->state),POWER_ONGOING ,POWER_UNKNOWN ); - -return -1 ; -} - -/** -* It is to check the governor and then set the original governor back if -* needed by writing the the sys file. -*/ -static int -power_set_governor_original (struct rte_power_info *pi) -{ -FILE *f; -int ret =-1 ; -char buf[BUFSIZ ]; -char fullpath[PATH_MAX ]; -char *s; -int val; - -snprintf (fullpath,sizeof (fullpath),POWER_SYSFILE_GOVERNOR , - pi->lcore_id); - f =fopen (fullpath,"rw+" ); -FOPEN_OR_ERR_RET (f, ret); - - s =fgets (buf,sizeof (buf), f); -FOPS_OR_NULL_GOTO (s, out); - -/* Check if the governor to be set is the same as current */ -if (strncmp (buf, pi->governor_ori,sizeof (pi->governor_ori)) ==0 ) { - ret =0 ; -POWER_DEBUG_TRACE ("Power management governor of lcore %u " -"has already been set to %s\n" , - pi->lcore_id, pi->governor_ori); +RTE_LOG (INFO ,POWER ,"Attempting to initialise VM power management...\n" ); + ret =rte_power_kvm_vm_init (lcore_id); +if (ret ==0 ) { +rte_power_set_env (PM_ENV_KVM_VM ); goto out; } - -/* Write back the original governor */ - val =fseek (f,0 ,SEEK_SET ); -FOPS_OR_ERR_GOTO (val, out); - - val =fputs (pi->governor_ori, f); -FOPS_OR_ERR_GOTO (val, out); - - ret =0 ; -RTE_LOG (INFO ,POWER ,"Power manamgement governor of lcore %u " -"has been set back to %s successfully\n" , - pi->lcore_id, pi->governor_ori); +RTE_LOG (ERR ,POWER ,"Unable to set Power Management Environment for lcore " +"%u\n" , lcore_id); out: -fclose (f); -return ret; }int rte_power_exit (unsigned lcore_id) { -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Lcore id %u can not exceeds %u\n" , - lcore_id,RTE_MAX_LCORE -1U ); -return -1 ; - } - pi = &lcore_power_info[lcore_id]; -if (rte_atomic32_cmpset (&(pi->state),POWER_USED ,POWER_ONGOING ) - ==0 ) { -RTE_LOG (INFO ,POWER ,"Power management of lcore %u is " -"not used\n" , lcore_id); -return -1 ; - } - -/* Close FD of setting freq */ -fclose (pi->f); - pi->f =NULL ; - -/* Set the governor back to the original */ -if (power_set_governor_original (pi) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot set the governor of %u back " -"to the original\n" , lcore_id); - goto fail; - } - -RTE_LOG (INFO ,POWER ,"Power management of lcore %u has exited from " -"'userspace' mode and been set back to the " -"original\n" , lcore_id); -rte_atomic32_cmpset (&(pi->state),POWER_ONGOING ,POWER_IDLE ); - -return 0 ; - -fail: -rte_atomic32_cmpset (&(pi->state),POWER_ONGOING ,POWER_UNKNOWN ); +if (global_default_env ==PM_ENV_ACPI_CPUFREQ ) +return rte_power_acpi_cpufreq_exit (lcore_id); +if (global_default_env ==PM_ENV_KVM_VM ) +return rte_power_kvm_vm_exit (lcore_id); +RTE_LOG (ERR ,POWER ,"Environment has not been set, unable to exit " +"gracefully\n" );return -1 ; -} - -uint32_t -rte_power_freqs (unsigned lcore_id,uint32_t *freqs,uint32_t num) -{ -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE || !freqs) { -RTE_LOG (ERR ,POWER ,"Invalid input parameter\n" ); -return 0 ; - } - - pi = &lcore_power_info[lcore_id]; -if (num < pi->nb_freqs) { -RTE_LOG (ERR ,POWER ,"Buffer size is not enough\n" ); -return 0 ; - } -rte_memcpy (freqs, pi->freqs, pi->nb_freqs *sizeof (uint32_t )); - -return pi->nb_freqs; -} - -uint32_t -rte_power_get_freq (unsigned lcore_id) -{ -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return RTE_POWER_INVALID_FREQ_INDEX ; - } - -return lcore_power_info[lcore_id].curr_idx; -} - -int -rte_power_set_freq (unsigned lcore_id,uint32_t index) -{ -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - -return set_freq_internal (&(lcore_power_info[lcore_id]), index); -} - -int -rte_power_freq_down (unsigned lcore_id) -{ -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - pi = &lcore_power_info[lcore_id]; -if (pi->curr_idx +1 == pi->nb_freqs) -return 0 ; - -/* Frequencies in the array are from high to low. */ -return set_freq_internal (pi, pi->curr_idx +1 ); } - -int -rte_power_freq_up (unsigned lcore_id) -{ -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - - pi = &lcore_power_info[lcore_id]; -if (pi->curr_idx ==0 ) -return 0 ; - -/* Frequencies in the array are from high to low. */ -return set_freq_internal (pi, pi->curr_idx -1 ); -} - -int -rte_power_freq_max (unsigned lcore_id) -{ -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - -/* Frequencies in the array are from high to low. */ -return set_freq_internal (&lcore_power_info[lcore_id],0 ); -} - -int -rte_power_freq_min (unsigned lcore_id) -{ -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - - pi = &lcore_power_info[lcore_id]; - -/* Frequencies in the array are from high to low. */ -return set_freq_internal (pi, pi->nb_freqs -1 ); -} -
lib/librte_power/rte_power.h +89 -31
214 lines not on this page · raw message
lib/librte_power/rte_power_acpi_cpufreq.c new file +545 -0
545 lines not on this page · raw message
lib/librte_power/rte_power_acpi_cpufreq.h new file +192 -0
192 lines not on this page · raw message
lib/librte_power/rte_power_common.h new file +39 -0
@@ -0,0 +1,39 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef RTE_POWER_COMMON_H_ +#define RTE_POWER_COMMON_H_ + +#define RTE_POWER_INVALID_FREQ_INDEX (~0) + +#endif /* RTE_POWER_COMMON_H_ */
lib/librte_power/rte_power_kvm_vm.c new file +135 -0
135 lines not on this page · raw message
lib/librte_power/rte_power_kvm_vm.h new file +179 -0
179 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v5 8/10 Packet format for VM Power Management(Host and Guest).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Provides a command packet format for host and guest.
--- lib/librte_power/channel_commands.h | 77 +++++++++++++++++++++++++++++++++++ 1 files changed, 77 insertions(+), 0 deletions(-) create mode 100644 lib/librte_power/channel_commands.h
lib/librte_power/channel_commands.h new file +77 -0
@@ -0,0 +1,77 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef CHANNEL_COMMANDS_H_ +#define CHANNEL_COMMANDS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdint.h> + +/* Maximum number of CPUs */ +#define CHANNEL_CMDS_MAX_CPUS 64 +#if CHANNEL_CMDS_MAX_CPUS >64 +#error Maximum number of cores is 64, overflow is guaranteed to \ + cause problems with VM Power Management +#endif + +/* Maximum number of channels per VM */ +#define CHANNEL_CMDS_MAX_VM_CHANNELS 64 + +/* Maximum number of channels per VM */ +#define CHANNEL_CMDS_MAX_VM_CHANNELS 64 + +/* Valid Commands */ +#define CPU_POWER 1 +#define CPU_POWER_CONNECT 2 + +/* CPU Power Command Scaling */ +#define CPU_POWER_SCALE_UP 1 +#define CPU_POWER_SCALE_DOWN 2 +#define CPU_POWER_SCALE_MAX 3 +#define CPU_POWER_SCALE_MIN 4 + +struct channel_packet { +uint64_t resource_id;/**< core_num, device */ +uint32_t unit;/**< scale down/up/min/max */ +uint32_t command;/**< Power, IO, etc */ +}; + + +#ifdef __cplusplus +} +#endif + +#endif /* CHANNEL_COMMANDS_H_ */
Signature · 1 line
1.7.4.1
PATCH v5 9/10 Build system integration for VM Power Management(Guest and Host)
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
librte_power now contains both rte_power_acpi_cpufreq and rte_power_kvm_vm implementations.
--- lib/librte_power/Makefile | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
lib/librte_power/Makefile +2 -1
@@ -37,7 +37,8 @@ LIB = librte_power.a CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 -fno-strict-aliasing # all source are stored in SRCS-y -SRCS-$(CONFIG_RTE_LIBRTE_POWER) := rte_power.c +SRCS-$(CONFIG_RTE_LIBRTE_POWER) := rte_power.c rte_power_acpi_cpufreq.c +SRCS-$(CONFIG_RTE_LIBRTE_POWER) += rte_power_kvm_vm.c guest_channel.c # install this header file SYMLINK-$(CONFIG_RTE_LIBRTE_POWER)-include := rte_power.h
Signature · 1 line
1.7.4.1
PATCH v5 10/10 VM Power Management Unit Tests
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Updated the unit tests to cover both librte_power implementations as well as the external API.
--- app/test/Makefile | 3 +- app/test/autotest_data.py | 26 ++ app/test/test_power.c | 445 +++--------------------------- app/test/test_power_acpi_cpufreq.c | 544 ++++++++++++++++++++++++++++++++++++ app/test/test_power_kvm_vm.c | 308 ++++++++++++++++++++ 5 files changed, 917 insertions(+), 409 deletions(-) create mode 100644 app/test/test_power_acpi_cpufreq.c create mode 100644 app/test/test_power_kvm_vm.c
app/test/Makefile +2 -1
@@ -120,7 +120,8 @@ endif SRCS-$(CONFIG_RTE_LIBRTE_METER) += test_meter.c SRCS-$(CONFIG_RTE_LIBRTE_KNI) += test_kni.c -SRCS-$(CONFIG_RTE_LIBRTE_POWER) += test_power.c +SRCS-$(CONFIG_RTE_LIBRTE_POWER) += test_power.c test_power_acpi_cpufreq.c +SRCS-$(CONFIG_RTE_LIBRTE_POWER) += test_power_kvm_vm.c SRCS-y += test_common.c SRCS-$(CONFIG_RTE_LIBRTE_IVSHMEM) += test_ivshmem.c
app/test/autotest_data.py +26 -0
@@ -425,6 +425,32 @@ non_parallel_test_group_list = [ ] }, { + "Prefix" : "power_acpi_cpufreq", + "Memory" : all_sockets(512), + "Tests" : + [ + { + "Name" : "Power ACPI cpufreq autotest", + "Command" : "power_acpi_cpufreq_autotest", + "Func" : default_autotest, + "Report" : None, + }, + ] +}, +{ + "Prefix" : "power_kvm_vm", + "Memory" : "512", + "Tests" : + [ + { + "Name" : "Power KVM VM autotest", + "Command" : "power_kvm_vm_autotest", + "Func" : default_autotest, + "Report" : None, + }, + ] +}, +{ "Prefix" : "lpm6", "Memory" : "512", "Tests" :
app/test/test_power.c +37 -408
474 lines not on this page · raw message
app/test/test_power_acpi_cpufreq.c new file +544 -0
544 lines not on this page · raw message
app/test/test_power_kvm_vm.c new file +308 -0
308 lines not on this page · raw message
Signature · 1 line
1.7.4.1
v6 cover Virtual Machine Power Management
In reply to a message not in the archiveVirtual Machine Power Management.
The following patches add two DPDK sample applications and an alternate implementation of librte_power for use in virtualized environments. The idea is to provide librte_power functionality from within a VM to address the lack of MSRs to facilitate frequency changes from within a VM. It is ideally suited for Haswell which provides per core frequency scaling.
The current librte_power affects frequency changes via the acpi-cpufreq 'userspace' power governor, accessed via sysfs.
General Overview:(more information in each patch that follows). The VM Power Management solution provides two components:
1)VM: Allows for the a DPDK application in a VM to reuse the librte_power interface. Each lcore opens a Virto-Serial endpoint channel to the host, where the re-implementation of librte_power simply forwards the requests for frequency change to a host based monitor. The host monitor itself uses librte_power.
Each lcore channel corresponds to a
serial device '/dev/virtio-ports/virtio.serial.port.poweragent.<lcore_num>' which is opened in non-blocking mode.
While each Virtual CPU can be mapped to multiple physical CPUs it is recommended that each vCPU should be mapped to a single core only.
2)Host: The host monitor is managed by a CLI, it allows for adding qemu/KVM virtual machines and associated channels to the monitor, manually changing CPU frequency, inspecting the state of VMs, vCPU to pCPU pinning and managing channels.
Host channel endpoints are Virto-Serial endpoints configured as AF_UNIX file sockets which follow a specific naming convention
i.e /tmp/powermonitor/<vm_name>.<channel_number>,
each channel has an 1:1 mapping to a VM endpoint
i.e. /dev/virtio-ports/virtio.serial.port.poweragent.<lcore_num>
Host channel endpoints are opened in non-blocking mode and are monitored via epoll. Requests over each channel to change frequency are forwarded to the original librte_power.
Channels must be manually configured as qemu-kvm command line arguments or libvirt domain definition(xml) e.g.
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</controller>
<channel type='unix'>
<source mode='bind' path='/tmp/powermonitor/<vm_name>.<channel_num>'/> <target type='virtio' name='virtio.serial.port.poweragent.<channel_num>/> <address type='virtio-serial' controller='0' bus='0' port='<N>'/>
</channel>
Where multiple channels can be configured by specifying multiple <channel> elements, by replacing <vm_name>, <channel_num>.
<N>(port number) should be incremented by 1 for each new channel element. More information on Virtio-Serial can be found here:
http://fedoraproject.org/wiki/Features/VirtioSerial
To enable the Hypervisor creation of channels, the host endpoint directory must be created with qemu permissions:
mkdir /tmp/powermonitor
chown qemu:qemu /tmp/powermonitor
The host application runs on two separate lcores:
Core N) CLI: For management of Virtual Machines adding channels to Monitor thread,
inspecting state and manually setting CPU frequency [PATCH 02/09]
Core N+1) Monitor Thread: An epoll based infinite loop that waits on channel events
from VMs and calls the corresponding librte_power functions.
A sample application is also provided to run on Virtual Machines, this application provides a CLI to manually set the frequency of a vCPU[PATCH 08/09]
The current l3fwd-power sample application can also be run on a VM.
Changes in V6:
Fixed typos and missing some identations and blank lines
Changes in V5:
Fixed default target in sample app Makefiles
Changes in V4:
Fixed double free of channel during VM shutdown.
Changes in V3:
Fixed crash in Guest CLI when host application is not running. Renamed #defines to be more specific to the module they belong Added vCPU pinning via CLI
Changes in V2:
Runtime selection of librte_power implementations. Updated Unit tests to cover librte_power changes. PATCH[0/3] was sent twice, again as PATCH[0/4] Miscellaneous fixes.
Alan Carew (10):
Channel Manager and Monitor for VM Power Management(Host). VM Power Management CLI(Host).
CPU Frequency Power Management(Host).
VM Power Management application and Makefile.
VM Power Management CLI(Guest).
VM communication channels for VM Power Management(Guest). librte_power common interface for Guest and Host Packet format for VM Power Management(Host and Guest). Build system integration for VM Power Management(Guest and Host) VM Power Management Unit Tests
app/test/Makefile | 3 +- app/test/autotest_data.py | 26 + app/test/test_power.c | 445 +---------- app/test/test_power_acpi_cpufreq.c | 544 +++++++++++++ app/test/test_power_kvm_vm.c | 308 ++++++++ examples/vm_power_manager/Makefile | 57 ++ examples/vm_power_manager/channel_manager.c | 808 ++++++++++++++++++++ examples/vm_power_manager/channel_manager.h | 314 ++++++++ examples/vm_power_manager/channel_monitor.c | 234 ++++++ examples/vm_power_manager/channel_monitor.h | 102 +++ examples/vm_power_manager/guest_cli/Makefile | 56 ++ examples/vm_power_manager/guest_cli/main.c | 88 +++ examples/vm_power_manager/guest_cli/main.h | 52 ++ .../guest_cli/vm_power_cli_guest.c | 156 ++++ .../guest_cli/vm_power_cli_guest.h | 55 ++ examples/vm_power_manager/main.c | 117 +++ examples/vm_power_manager/main.h | 52 ++ examples/vm_power_manager/power_manager.c | 253 ++++++ examples/vm_power_manager/power_manager.h | 188 +++++ examples/vm_power_manager/vm_power_cli.c | 673 ++++++++++++++++ examples/vm_power_manager/vm_power_cli.h | 47 ++ lib/librte_power/Makefile | 3 +- lib/librte_power/channel_commands.h | 77 ++ lib/librte_power/guest_channel.c | 162 ++++ lib/librte_power/guest_channel.h | 89 +++ lib/librte_power/rte_power.c | 540 ++------------ lib/librte_power/rte_power.h | 120 +++- lib/librte_power/rte_power_acpi_cpufreq.c | 545 +++++++++++++ lib/librte_power/rte_power_acpi_cpufreq.h | 192 +++++ lib/librte_power/rte_power_common.h | 39 + lib/librte_power/rte_power_kvm_vm.c | 136 ++++ lib/librte_power/rte_power_kvm_vm.h | 179 +++++ 32 files changed, 5748 insertions(+), 912 deletions(-) create mode 100644 app/test/test_power_acpi_cpufreq.c create mode 100644 app/test/test_power_kvm_vm.c create mode 100644 examples/vm_power_manager/Makefile create mode 100644 examples/vm_power_manager/channel_manager.c create mode 100644 examples/vm_power_manager/channel_manager.h create mode 100644 examples/vm_power_manager/channel_monitor.c create mode 100644 examples/vm_power_manager/channel_monitor.h create mode 100644 examples/vm_power_manager/guest_cli/Makefile create mode 100644 examples/vm_power_manager/guest_cli/main.c create mode 100644 examples/vm_power_manager/guest_cli/main.h create mode 100644 examples/vm_power_manager/guest_cli/vm_power_cli_guest.c create mode 100644 examples/vm_power_manager/guest_cli/vm_power_cli_guest.h create mode 100644 examples/vm_power_manager/main.c create mode 100644 examples/vm_power_manager/main.h create mode 100644 examples/vm_power_manager/power_manager.c create mode 100644 examples/vm_power_manager/power_manager.h create mode 100644 examples/vm_power_manager/vm_power_cli.c create mode 100644 examples/vm_power_manager/vm_power_cli.h create mode 100644 lib/librte_power/channel_commands.h create mode 100644 lib/librte_power/guest_channel.c create mode 100644 lib/librte_power/guest_channel.h create mode 100644 lib/librte_power/rte_power_acpi_cpufreq.c create mode 100644 lib/librte_power/rte_power_acpi_cpufreq.h create mode 100644 lib/librte_power/rte_power_common.h create mode 100644 lib/librte_power/rte_power_kvm_vm.c create mode 100644 lib/librte_power/rte_power_kvm_vm.h
Signature · 1 line
1.7.4.1
A message that is not in the archive (sent off-list or not yet received): 1416932291-13162-1-git-send-email-pablo.de.lara.guarch-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org
PATCH v6 1/10 Channel Manager and Monitor for VM Power Management(Host).
From: Alan Carew <[email protected]>
The manager is responsible for adding communications channels to the Monitor thread, tracking and reporting VM state and employs the libvirt API for synchronization with the KVM Hypervisor. The manager interacts with the Hypervisor to discover the mapping of virtual CPUS(vCPUs) to the host physical CPUS(pCPUs) and to inspect the VM running state.
The manager provides the following functionality to the CLI:
1) Connect to a libvirtd instance, default: qemu:///system
2) Add a VM to an internal list, each VM is identified by a "name" which must correspond a valid libvirt Domain Name.
3) Add communication channels associated with a VM to the epoll based Monitor thread.
The channels must exist and be in the form of:
/tmp/powermonitor/<vm_name>.<channel_number>. Each channel is a Virtio-Serial endpoint configured as an AF_UNIX file socket and opened in non-blocking mode.
Each VM can have a maximum of 64 channels associated with it.
4) Disable or re-enable VM communication channels, channels once added to the Monitor thread remain in that threads control, however acting on channel requests can be disabled and renabled via CLI.
The monitor is an epoll based infinite loop running in a separate thread that waits on channel events from VMs and calls the corresponding functions. Channel definitions from the manager are registered via the epoll event opaque pointer when calling epoll_ctl(EPOLL_CTL_ADD), this allows for obtaining the channels file descriptor for reading EPOLLIN events and mapping the vCPU to pCPU(s) associated with a request from a particular VM.
--- examples/vm_power_manager/channel_manager.c | 808 +++++++++++++++++++++++++++ examples/vm_power_manager/channel_manager.h | 314 +++++++++++ examples/vm_power_manager/channel_monitor.c | 234 ++++++++ examples/vm_power_manager/channel_monitor.h | 102 ++++ 4 files changed, 1458 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/channel_manager.c create mode 100644 examples/vm_power_manager/channel_manager.h create mode 100644 examples/vm_power_manager/channel_monitor.c create mode 100644 examples/vm_power_manager/channel_monitor.h
examples/vm_power_manager/channel_manager.c new file +808 -0
@@ -0,0 +1,808 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/un.h> +#include <fcntl.h> +#include <unistd.h> +#include <inttypes.h> +#include <dirent.h> +#include <errno.h> + +#include <sys/queue.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/select.h> + +#include <rte_config.h> +#include <rte_malloc.h> +#include <rte_memory.h> +#include <rte_mempool.h> +#include <rte_log.h> +#include <rte_atomic.h> +#include <rte_spinlock.h> + +#include <libvirt/libvirt.h> + +#include "channel_manager.h" +#include "channel_commands.h" +#include "channel_monitor.h" + + +#define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1 + +#define ITERATIVE_BITMASK_CHECK_64 (mask_u64b, i) \ + for (i = 0; mask_u64b; mask_u64b &= ~(1ULL << i++)) \ + if ((mask_u64b >> i) & 1) \ + +/* Global pointer to libvirt connection */ +static virConnectPtr global_vir_conn_ptr; + +static unsigned char *global_cpumaps; +static virVcpuInfo *global_vircpuinfo; +static size_t global_maplen; + +static unsigned global_n_host_cpus; + +/* +* Represents a single Virtual Machine +*/ +struct virtual_machine_info { +char name[CHANNEL_MGR_MAX_NAME_LEN ]; +rte_atomic64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS ]; +struct channel_info *channels[CHANNEL_CMDS_MAX_VM_CHANNELS ]; +uint64_t channel_mask; +uint8_t num_channels; +enum vm_status status; +virDomainPtr domainPtr; +virDomainInfo info; +rte_spinlock_t config_spinlock; +LIST_ENTRY (virtual_machine_info) vms_info; +}; + +LIST_HEAD(, virtual_machine_info) vm_list_head; + +static struct virtual_machine_info * +find_domain_by_name (const char *name) +{ +struct virtual_machine_info *info; + LIST_FOREACH(info, &vm_list_head, vms_info) { +if (!strncmp (info->name, name,CHANNEL_MGR_MAX_NAME_LEN -1 )) +return info; + } +return NULL ; +} + +static int +update_pcpus_mask (struct virtual_machine_info *vm_info) +{ +virVcpuInfoPtr cpuinfo; +unsigned i, j; +int n_vcpus; +uint64_t mask; + +memset (global_cpumaps,0 ,CHANNEL_CMDS_MAX_CPUS *global_maplen); + +if (!virDomainIsActive (vm_info->domainPtr)) { + n_vcpus =virDomainGetVcpuPinInfo (vm_info->domainPtr, + vm_info->info.nrVirtCpu, global_cpumaps, global_maplen, +VIR_DOMAIN_AFFECT_CONFIG ); +if (n_vcpus <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting vCPU info for " +"in-active VM '%s'\n" , vm_info->name); +return -1 ; + } + goto update_pcpus; + } + +memset (global_vircpuinfo,0 ,sizeof (*global_vircpuinfo)* +CHANNEL_CMDS_MAX_CPUS ); + + cpuinfo = global_vircpuinfo; + + n_vcpus =virDomainGetVcpus (vm_info->domainPtr, cpuinfo, +CHANNEL_CMDS_MAX_CPUS , global_cpumaps, global_maplen); +if (n_vcpus <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting vCPU info for " +"active VM '%s'\n" , vm_info->name); +return -1 ; + } +update_pcpus: +if (n_vcpus >=CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Number of vCPUS(%u) is out of range " +"0...%d\n" , n_vcpus,CHANNEL_CMDS_MAX_CPUS -1 ); +return -1 ; + } +if (n_vcpus != vm_info->info.nrVirtCpu) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Updating the number of vCPUs for VM '%s" +" from %d -> %d\n" , vm_info->name, vm_info->info.nrVirtCpu, + n_vcpus); + vm_info->info.nrVirtCpu = n_vcpus; + } +for (i =0 ; i < vm_info->info.nrVirtCpu; i++) { + mask =0 ; +for (j =0 ; j < global_n_host_cpus; j++) { +if (VIR_CPU_USABLE (global_cpumaps, global_maplen, i, j) >0 ) { + mask |=1ULL << j; + } + } +rte_atomic64_set (&vm_info->pcpu_mask[i], mask); + } +return 0 ; +} + +int +set_pcpus_mask (char *vm_name,unsigned vcpu,uint64_t core_mask) +{ +unsigned i =0 ; +int flags =VIR_DOMAIN_AFFECT_LIVE |VIR_DOMAIN_AFFECT_CONFIG ; +struct virtual_machine_info *vm_info; +uint64_t mask = core_mask; + +if (vcpu >=CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"vCPU(%u) exceeds max allowable(%d)\n" , + vcpu,CHANNEL_CMDS_MAX_CPUS -1 ); +return -1 ; + } + + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM '%s' not found\n" , vm_name); +return -1 ; + } + +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to set vCPU(%u) to pCPU " +"mask(0x%" PRIx64") for VM '%s', VM is not active\n" , + vcpu, core_mask, vm_info->name); +return -1 ; + } + +if (vcpu >= vm_info->info.nrVirtCpu) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"vCPU(%u) exceeds the assigned number of " +"vCPUs(%u)\n" , vcpu, vm_info->info.nrVirtCpu); +return -1 ; + } +memset (global_cpumaps,0 ,CHANNEL_CMDS_MAX_CPUS * global_maplen); + ITERATIVE_BITMASK_CHECK_64(mask, i) { +VIR_USE_CPU (global_cpumaps, i); +if (i >= global_n_host_cpus) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"CPU(%u) exceeds the available " +"number of CPUs(%u)\n" , i, global_n_host_cpus); +return -1 ; + } + } +if (virDomainPinVcpuFlags (vm_info->domainPtr, vcpu, global_cpumaps, + global_maplen, flags) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to set vCPU(%u) to pCPU " +"mask(0x%" PRIx64") for VM '%s'\n" , vcpu, core_mask, + vm_info->name); +return -1 ; + } +rte_atomic64_set (&vm_info->pcpu_mask[vcpu], core_mask); +return 0 ; + +} + +int +set_pcpu (char *vm_name,unsigned vcpu,unsigned core_num) +{ +uint64_t mask =1ULL << core_num; + +return set_pcpus_mask (vm_name, vcpu, mask); +} + +uint64_t +get_pcpus_mask (struct channel_info *chan_info,unsigned vcpu) +{ +struct virtual_machine_info *vm_info = + (struct virtual_machine_info *)chan_info->priv_info; +return rte_atomic64_read (&vm_info->pcpu_mask[vcpu]); +} + +static inline int +channel_exists (struct virtual_machine_info *vm_info,unsigned channel_num) +{ +rte_spinlock_lock (&(vm_info->config_spinlock)); +if (vm_info->channel_mask & (1ULL << channel_num)) { +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 1 ; + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 0 ; +} + + + +static int +open_non_blocking_channel (struct channel_info *info) +{ +int ret, flags; +struct sockaddr_un sock_addr; +fd_set soc_fd_set; +struct timeval tv; + + info->fd =socket (AF_UNIX ,SOCK_STREAM ,0 ); +if (info->fd ==-1 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error(%s) creating socket for '%s'\n" , +strerror (errno), + info->channel_path); +return -1 ; + } + sock_addr.sun_family =AF_UNIX ; +memcpy (&sock_addr.sun_path, info->channel_path, +strlen (info->channel_path)+1 ); + +/* Get current flags */ + flags =fcntl (info->fd,F_GETFL ,0 ); +if (flags <0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) fcntl get flags socket for" +"'%s'\n" ,strerror (errno), info->channel_path); +return 1 ; + } +/* Set to Non Blocking */ + flags |=O_NONBLOCK ; +if (fcntl (info->fd,F_SETFL , flags) <0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) setting non-blocking " +"socket for '%s'\n" ,strerror (errno), info->channel_path); +return -1 ; + } + ret =connect (info->fd, (struct sockaddr *)&sock_addr, +sizeof (sock_addr)); +if (ret <0 ) { +/* ECONNREFUSED error is given when VM is not active */ +if (errno ==ECONNREFUSED ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"VM is not active or has not " +"activated its endpoint to channel %s\n" , + info->channel_path); +return -1 ; + } +/* Wait for tv_sec if in progress */ +else if (errno ==EINPROGRESS ) { + tv.tv_sec =2 ; + tv.tv_usec =0 ; +FD_ZERO (&soc_fd_set); +FD_SET (info->fd, &soc_fd_set); +if (select (info->fd+1 ,NULL , &soc_fd_set,NULL , &tv) >0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Timeout or error on channel " +"'%s'\n" , info->channel_path); +return -1 ; + } + }else { +/* Any other error */ +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) connecting socket" +" for '%s'\n" ,strerror (errno), info->channel_path); +return -1 ; + } + } +return 0 ; +} + +static int +setup_channel_info (struct virtual_machine_info **vm_info_dptr, +struct channel_info **chan_info_dptr,unsigned channel_num) +{ +struct channel_info *chan_info = *chan_info_dptr; +struct virtual_machine_info *vm_info = *vm_info_dptr; + + chan_info->channel_num = channel_num; + chan_info->priv_info = (void *)vm_info; + chan_info->status =CHANNEL_MGR_CHANNEL_DISCONNECTED ; +if (open_non_blocking_channel (chan_info) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Could not open channel: " +"'%s' for VM '%s'\n" , + chan_info->channel_path, vm_info->name); +return -1 ; + } +if (add_channel_to_monitor (&chan_info) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Could add channel: " +"'%s' to epoll ctl for VM '%s'\n" , + chan_info->channel_path, vm_info->name); +return -1 ; + + } +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->num_channels++; + vm_info->channel_mask |=1ULL << channel_num; + vm_info->channels[channel_num] = chan_info; + chan_info->status =CHANNEL_MGR_CHANNEL_CONNECTED ; +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 0 ; +} + +int +add_all_channels (const char *vm_name) +{ +DIR *d; +struct dirent *dir; +struct virtual_machine_info *vm_info; +struct channel_info *chan_info; +char *token, *remaining, *tail_ptr; +char socket_name[PATH_MAX ]; +unsigned channel_num; +int num_channels_enabled =0 ; + +/* verify VM exists */ + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' not found" +" during channel discovery\n" , vm_name); +return 0 ; + } +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' is not active\n" , vm_name); + vm_info->status =CHANNEL_MGR_VM_INACTIVE ; +return 0 ; + } + d =opendir (CHANNEL_MGR_SOCKET_PATH ); +if (d ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error opening directory '%s': %s\n" , +CHANNEL_MGR_SOCKET_PATH ,strerror (errno)); +return -1 ; + } +while ((dir =readdir (d)) !=NULL ) { +if (!strncmp (dir->d_name,"." ,1 ) || + !strncmp (dir->d_name,".." ,2 )) +continue ; + +snprintf (socket_name,sizeof (socket_name),"%s" , dir->d_name); + remaining = socket_name; +/* Extract vm_name from "<vm_name>.<channel_num>" */ + token =strsep (&remaining,"." ); +if (remaining ==NULL ) +continue ; +if (strncmp (vm_name, token,CHANNEL_MGR_MAX_NAME_LEN )) +continue ; + +/* remaining should contain only <channel_num> */ + errno =0 ; + channel_num = (unsigned )strtol (remaining, &tail_ptr,0 ); +if ((errno !=0 ) || (remaining[0 ] =='\0' ) || + (*tail_ptr !='\0' ) || tail_ptr ==NULL ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Malformed channel name" +"'%s' found it should be in the form of " +"'<guest_name>.<channel_num>(decimal)'\n" , + dir->d_name); +continue ; + } +if (channel_num >=CHANNEL_CMDS_MAX_VM_CHANNELS ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Channel number(%u) is " +"greater than max allowable: %d, skipping '%s%s'\n" , + channel_num,CHANNEL_CMDS_MAX_VM_CHANNELS -1 , +CHANNEL_MGR_SOCKET_PATH , dir->d_name); +continue ; + } +/* if channel has not been added previously */ +if (channel_exists (vm_info, channel_num)) +continue ; + + chan_info =rte_malloc (NULL ,sizeof (*chan_info), +CACHE_LINE_SIZE ); +if (chan_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for " +"channel '%s%s'\n" ,CHANNEL_MGR_SOCKET_PATH , dir->d_name); +continue ; + } + +snprintf (chan_info->channel_path, +sizeof (chan_info->channel_path),"%s%s" , +CHANNEL_MGR_SOCKET_PATH , dir->d_name); + +if (setup_channel_info (&vm_info, &chan_info, channel_num) <0 ) { +rte_free (chan_info); +continue ; + } + + num_channels_enabled++; + } +closedir (d); +return num_channels_enabled; +} + +int +add_channels (const char *vm_name,unsigned *channel_list, +unsigned len_channel_list) +{ +struct virtual_machine_info *vm_info; +struct channel_info *chan_info; +char socket_path[PATH_MAX ]; +unsigned i; +int num_channels_enabled =0 ; + + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } + +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' is not active\n" , vm_name); + vm_info->status =CHANNEL_MGR_VM_INACTIVE ; +return 0 ; + } + +for (i =0 ; i < len_channel_list; i++) { + +if (channel_list[i] >=CHANNEL_CMDS_MAX_VM_CHANNELS ) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Channel(%u) is out of range " +"0...%d\n" , channel_list[i], +CHANNEL_CMDS_MAX_VM_CHANNELS -1 ); +continue ; + } +if (channel_exists (vm_info, channel_list[i])) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Channel already exists, skipping " +"'%s.%u'\n" , vm_name, i); +continue ; + } + +snprintf (socket_path,sizeof (socket_path),"%s%s.%u" , +CHANNEL_MGR_SOCKET_PATH , vm_name, channel_list[i]); + errno =0 ; +if (access (socket_path,F_OK ) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channel path '%s' error: " +"%s\n" , socket_path,strerror (errno)); +continue ; + } + chan_info =rte_malloc (NULL ,sizeof (*chan_info), +CACHE_LINE_SIZE ); +if (chan_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for " +"channel '%s'\n" , socket_path); +continue ; + } +snprintf (chan_info->channel_path, +sizeof (chan_info->channel_path),"%s%s.%u" , +CHANNEL_MGR_SOCKET_PATH , vm_name, channel_list[i]); +if (setup_channel_info (&vm_info, &chan_info, channel_list[i]) <0 ) { +rte_free (chan_info); +continue ; + } + num_channels_enabled++; + + } +return num_channels_enabled; +} + +int +remove_channel (struct channel_info **chan_info_dptr) +{ +struct virtual_machine_info *vm_info; +struct channel_info *chan_info = *chan_info_dptr; + +close (chan_info->fd); + + vm_info = (struct virtual_machine_info *)chan_info->priv_info; + +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->channel_mask &= ~(1ULL << chan_info->channel_num); + vm_info->num_channels--; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +rte_free (chan_info); +return 0 ; +} + +int +set_channel_status_all (const char *vm_name,enum channel_status status) +{ +struct virtual_machine_info *vm_info; +unsigned i; +uint64_t mask; +int num_channels_changed =0 ; + +if (!(status ==CHANNEL_MGR_CHANNEL_CONNECTED || + status ==CHANNEL_MGR_CHANNEL_DISABLED )) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channels can only be enabled or " +"disabled: Unable to change status for VM '%s'\n" , vm_name); + } + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to disable channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } + +rte_spinlock_lock (&(vm_info->config_spinlock)); + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { + vm_info->channels[i]->status = status; + num_channels_changed++; + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return num_channels_changed; + +} + +int +set_channel_status (const char *vm_name,unsigned *channel_list, +unsigned len_channel_list,enum channel_status status) +{ +struct virtual_machine_info *vm_info; +unsigned i; +int num_channels_changed =0 ; + +if (!(status ==CHANNEL_MGR_CHANNEL_CONNECTED || + status ==CHANNEL_MGR_CHANNEL_DISABLED )) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channels can only be enabled or " +"disabled: Unable to change status for VM '%s'\n" , vm_name); + } + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } +for (i =0 ; i < len_channel_list; i++) { +if (channel_exists (vm_info, channel_list[i])) { +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->channels[channel_list[i]]->status = status; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + num_channels_changed++; + } + } +return num_channels_changed; +} + +int +get_info_vm (const char *vm_name,struct vm_info *info) +{ +struct virtual_machine_info *vm_info; +unsigned i, channel_num =0 ; +uint64_t mask; + + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM '%s' not found\n" , vm_name); +return -1 ; + } + info->status =CHANNEL_MGR_VM_ACTIVE ; +if (!virDomainIsActive (vm_info->domainPtr)) + info->status =CHANNEL_MGR_VM_INACTIVE ; + +rte_spinlock_lock (&(vm_info->config_spinlock)); + + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { + info->channels[channel_num].channel_num = i; +memcpy (info->channels[channel_num].channel_path, + vm_info->channels[i]->channel_path,PATH_MAX ); + info->channels[channel_num].status = vm_info->channels[i]->status; + info->channels[channel_num].fd = vm_info->channels[i]->fd; + channel_num++; + } + + info->num_channels = channel_num; + info->num_vcpus = vm_info->info.nrVirtCpu; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +memcpy (info->name, vm_info->name,sizeof (vm_info->name)); +for (i =0 ; i < info->num_vcpus; i++) { + info->pcpu_mask[i] =rte_atomic64_read (&vm_info->pcpu_mask[i]); + } +return 0 ; +} + +int +add_vm (const char *vm_name) +{ +struct virtual_machine_info *new_domain; +virDomainPtr dom_ptr; +int i; + +if (find_domain_by_name (vm_name) !=NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add VM: VM '%s' " +"already exists\n" , vm_name); +return -1 ; + } + +if (global_vir_conn_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"No connection to hypervisor exists\n" ); +return -1 ; + } + dom_ptr =virDomainLookupByName (global_vir_conn_ptr, vm_name); +if (dom_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error on VM lookup with libvirt: " +"VM '%s' not found\n" , vm_name); +return -1 ; + } + + new_domain =rte_malloc ("virtual_machine_info" ,sizeof (*new_domain), +CACHE_LINE_SIZE ); +if (new_domain ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to allocate memory for VM " +"info\n" ); +return -1 ; + } + new_domain->domainPtr = dom_ptr; +if (virDomainGetInfo (new_domain->domainPtr, &new_domain->info) !=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to get libvirt VM info\n" ); +rte_free (new_domain); +return -1 ; + } +if (new_domain->info.nrVirtCpu >CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error the number of virtual CPUs(%u) is " +"greater than allowable(%d)\n" , new_domain->info.nrVirtCpu, +CHANNEL_CMDS_MAX_CPUS ); +rte_free (new_domain); +return -1 ; + } + +for (i =0 ; i <CHANNEL_CMDS_MAX_CPUS ; i++) { +rte_atomic64_init (&new_domain->pcpu_mask[i]); + } +if (update_pcpus_mask (new_domain) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting physical CPU pinning\n" ); +rte_free (new_domain); +return -1 ; + } +strncpy (new_domain->name, vm_name,sizeof (new_domain->name)); + new_domain->channel_mask =0 ; + new_domain->num_channels =0 ; + +if (!virDomainIsActive (dom_ptr)) + new_domain->status =CHANNEL_MGR_VM_INACTIVE ; +else + new_domain->status =CHANNEL_MGR_VM_ACTIVE ; + +rte_spinlock_init (&(new_domain->config_spinlock)); +LIST_INSERT_HEAD (&vm_list_head, new_domain, vms_info); +return 0 ; +} + +int +remove_vm (const char *vm_name) +{ +struct virtual_machine_info *vm_info =find_domain_by_name (vm_name); + +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to remove VM: VM '%s' " +"not found\n" , vm_name); +return -1 ; + } +rte_spinlock_lock (&vm_info->config_spinlock); +if (vm_info->num_channels !=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to remove VM '%s', there are " +"%" PRId8" channels still active\n" , + vm_name, vm_info->num_channels); +rte_spinlock_unlock (&vm_info->config_spinlock); +return -1 ; + } +LIST_REMOVE (vm_info, vms_info); +rte_spinlock_unlock (&vm_info->config_spinlock); +rte_free (vm_info); +return 0 ; +} + +static void +disconnect_hypervisor (void ) +{ +if (global_vir_conn_ptr !=NULL ) { +virConnectClose (global_vir_conn_ptr); + global_vir_conn_ptr =NULL ; + } +} + +static int +connect_hypervisor (const char *path) +{ +if (global_vir_conn_ptr !=NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error connecting to %s, connection" +"already established\n" , path); +return -1 ; + } + global_vir_conn_ptr =virConnectOpen (path); +if (global_vir_conn_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error failed to open connection to " +"Hypervisor '%s'\n" , path); +return -1 ; + } +return 0 ; +} + +int +channel_manager_init (const char *path) +{ +int n_cpus; + +LIST_INIT (&vm_list_head); +if (connect_hypervisor (path) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to initialize channel manager\n" ); +return -1 ; + } + + global_maplen =VIR_CPU_MAPLEN (CHANNEL_CMDS_MAX_CPUS ); + + global_vircpuinfo =rte_zmalloc (NULL ,sizeof (*global_vircpuinfo) * +CHANNEL_CMDS_MAX_CPUS ,CACHE_LINE_SIZE ); +if (global_vircpuinfo ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for CPU Info\n" ); + goto error; + } + global_cpumaps =rte_zmalloc (NULL ,CHANNEL_CMDS_MAX_CPUS * global_maplen, +CACHE_LINE_SIZE ); +if (global_cpumaps ==NULL ) { + goto error; + } + + n_cpus =virNodeGetCPUMap (global_vir_conn_ptr,NULL ,NULL ,0 ); +if (n_cpus <=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to get the number of Host " +"CPUs\n" ); + goto error; + } + global_n_host_cpus = (unsigned )n_cpus; + +if (global_n_host_cpus >CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"The number of host CPUs(%u) exceeds the " +"maximum of %u\n" , global_n_host_cpus,CHANNEL_CMDS_MAX_CPUS ); + goto error; + + } + +return 0 ; +error: +disconnect_hypervisor (); +return -1 ; +} + +void +channel_manager_exit (void ) +{ +unsigned i; +uint64_t mask; +struct virtual_machine_info *vm_info; + + LIST_FOREACH(vm_info, &vm_list_head, vms_info) { + +rte_spinlock_lock (&(vm_info->config_spinlock)); + + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { +remove_channel_from_monitor (vm_info->channels[i]); +close (vm_info->channels[i]->fd); +rte_free (vm_info->channels[i]); + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +LIST_REMOVE (vm_info, vms_info); +rte_free (vm_info); + } + +if (global_cpumaps !=NULL ) +rte_free (global_cpumaps); +if (global_vircpuinfo !=NULL ) +rte_free (global_vircpuinfo); +disconnect_hypervisor (); +}
examples/vm_power_manager/channel_manager.h new file +314 -0
314 lines not on this page · raw message
examples/vm_power_manager/channel_monitor.c new file +234 -0
234 lines not on this page · raw message
examples/vm_power_manager/channel_monitor.h new file +102 -0
102 lines not on this page · raw message
Signature · 1 line
1.7.4.1
A message that is not in the archive (sent off-list or not yet received): 1416932291-13162-2-git-send-email-pablo.de.lara.guarch-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org
Re: [PATCH v6 01/10] Channel Manager and Monitor for VM Power Management(Host).
Quoted 107 lines · Pablo de Lara
From: Alan Carew <[email protected]>
The manager is responsible for adding communications channels to the Monitor thread, tracking and reporting VM state and employs the libvirt API for synchronization with the KVM Hypervisor. The manager interacts with the Hypervisor to discover the mapping of virtual CPUS(vCPUs) to the host physical CPUS(pCPUs) and to inspect the VM running state.
The manager provides the following functionality to the CLI:
1) Connect to a libvirtd instance, default: qemu:///system
2) Add a VM to an internal list, each VM is identified by a "name" which must correspond a valid libvirt Domain Name.
3) Add communication channels associated with a VM to the epoll based Monitor thread.
The channels must exist and be in the form of:
/tmp/powermonitor/<vm_name>.<channel_number>. Each channel is a Virtio-Serial endpoint configured as anAF_UNIXfile socket and opened in non-blocking mode.
Each VM can have a maximum of 64 channels associated with it.
4) Disable or re-enable VM communication channels, channels once added to the Monitor thread remain in that threads control, however acting on channel requests can be disabled and renabled via CLI.The monitor is an epoll based infinite loop running in a separate thread that waits on channel events from VMs and calls the corresponding functions. Channel definitions from the manager are registered via the epoll event opaque pointer when calling
epoll_ctl(EPOLL_CTL_ADD), this allows for obtaining the channels file descriptor for reading EPOLLIN events and mapping the vCPU to pCPU(s) associated with a request from a particular VM.Signed-off-by Alan Carew <[email protected]>Signed-off-by Pablo de Lara <[email protected]>--- examples/vm_power_manager/channel_manager.c | 808 +++++++++++++++++++++++++++ examples/vm_power_manager/channel_manager.h | 314 +++++++++++ examples/vm_power_manager/channel_monitor.c | 234 ++++++++ examples/vm_power_manager/channel_monitor.h | 102 ++++ 4 files changed, 1458 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/channel_manager.c create mode 100644 examples/vm_power_manager/channel_manager.h create mode 100644 examples/vm_power_manager/channel_monitor.c create mode 100644 examples/vm_power_manager/channel_monitor.hexamples/vm_power_manager/channel_manager.c new file +58
-0@@ -0,0 +1,808 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/un.h> +#include <fcntl.h> +#include <unistd.h> +#include <inttypes.h> +#include <dirent.h> +#include <errno.h> + +#include <sys/queue.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/select.h> + +#include <rte_config.h> +#include <rte_malloc.h> +#include <rte_memory.h> +#include <rte_mempool.h> +#include <rte_log.h> +#include <rte_atomic.h> +#include <rte_spinlock.h> + +#include <libvirt/libvirt.h> + +#include "channel_manager.h"
examples/vm_power_manager/channel_manager.c line 68
Quoted 10 lines
examples/vm_power_manager/channel_manager.c new file +10
-0+#include "channel_commands.h" +#include "channel_monitor.h" + + +#define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1 + +#define ITERATIVE_BITMASK_CHECK_64 (mask_u64b, i) \ + for (i = 0; mask_u64b; mask_u64b &= ~(1ULL << i++)) \ + if ((mask_u64b >> i) & 1) \ +
You specify masks as 64bit entries throughout this code, is that sufficient? IIRC someone was just undertaking some work to allow for systems that supported larger than 64 bit system. I know linux (and I'm sure bsd) contain a bitmask or cpumask that is of variable length so that an arbitrary number of cpus can be specified.
Quoted 157 lines
examples/vm_power_manager/channel_manager.c new file +157
-0+/* Global pointer to libvirt connection */ +static virConnectPtr global_vir_conn_ptr; + +static unsigned char *global_cpumaps; +static virVcpuInfo *global_vircpuinfo; +static size_t global_maplen; + +static unsigned global_n_host_cpus; + +/* +* Represents a single Virtual Machine +*/ +struct virtual_machine_info { +char name[CHANNEL_MGR_MAX_NAME_LEN ]; +rte_atomic64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS ]; +struct channel_info *channels[CHANNEL_CMDS_MAX_VM_CHANNELS ]; +uint64_t channel_mask; +uint8_t num_channels; +enum vm_status status; +virDomainPtr domainPtr; +virDomainInfo info; +rte_spinlock_t config_spinlock; +LIST_ENTRY (virtual_machine_info) vms_info; +}; + +LIST_HEAD(, virtual_machine_info) vm_list_head; + +static struct virtual_machine_info * +find_domain_by_name (const char *name) +{ +struct virtual_machine_info *info; + LIST_FOREACH(info, &vm_list_head, vms_info) { +if (!strncmp (info->name, name,CHANNEL_MGR_MAX_NAME_LEN -1 )) +return info; + } +return NULL ; +} + +static int +update_pcpus_mask (struct virtual_machine_info *vm_info) +{ +virVcpuInfoPtr cpuinfo; +unsigned i, j; +int n_vcpus; +uint64_t mask; + +memset (global_cpumaps,0 ,CHANNEL_CMDS_MAX_CPUS *global_maplen); + +if (!virDomainIsActive (vm_info->domainPtr)) { + n_vcpus =virDomainGetVcpuPinInfo (vm_info->domainPtr, + vm_info->info.nrVirtCpu, global_cpumaps, global_maplen, +VIR_DOMAIN_AFFECT_CONFIG ); +if (n_vcpus <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting vCPU info for " +"in-active VM '%s'\n" , vm_info->name); +return -1 ; + } + goto update_pcpus; + } + +memset (global_vircpuinfo,0 ,sizeof (*global_vircpuinfo)* +CHANNEL_CMDS_MAX_CPUS ); + + cpuinfo = global_vircpuinfo; + + n_vcpus =virDomainGetVcpus (vm_info->domainPtr, cpuinfo, +CHANNEL_CMDS_MAX_CPUS , global_cpumaps, global_maplen); +if (n_vcpus <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting vCPU info for " +"active VM '%s'\n" , vm_info->name); +return -1 ; + } +update_pcpus: +if (n_vcpus >=CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Number of vCPUS(%u) is out of range " +"0...%d\n" , n_vcpus,CHANNEL_CMDS_MAX_CPUS -1 ); +return -1 ; + } +if (n_vcpus != vm_info->info.nrVirtCpu) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Updating the number of vCPUs for VM '%s" +" from %d -> %d\n" , vm_info->name, vm_info->info.nrVirtCpu, + n_vcpus); + vm_info->info.nrVirtCpu = n_vcpus; + } +for (i =0 ; i < vm_info->info.nrVirtCpu; i++) { + mask =0 ; +for (j =0 ; j < global_n_host_cpus; j++) { +if (VIR_CPU_USABLE (global_cpumaps, global_maplen, i, j) >0 ) { + mask |=1ULL << j; + } + } +rte_atomic64_set (&vm_info->pcpu_mask[i], mask); + } +return 0 ; +} + +int +set_pcpus_mask (char *vm_name,unsigned vcpu,uint64_t core_mask) +{ +unsigned i =0 ; +int flags =VIR_DOMAIN_AFFECT_LIVE |VIR_DOMAIN_AFFECT_CONFIG ; +struct virtual_machine_info *vm_info; +uint64_t mask = core_mask; + +if (vcpu >=CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"vCPU(%u) exceeds max allowable(%d)\n" , + vcpu,CHANNEL_CMDS_MAX_CPUS -1 ); +return -1 ; + } + + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM '%s' not found\n" , vm_name); +return -1 ; + } + +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to set vCPU(%u) to pCPU " +"mask(0x%" PRIx64") for VM '%s', VM is not active\n" , + vcpu, core_mask, vm_info->name); +return -1 ; + } + +if (vcpu >= vm_info->info.nrVirtCpu) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"vCPU(%u) exceeds the assigned number of " +"vCPUs(%u)\n" , vcpu, vm_info->info.nrVirtCpu); +return -1 ; + } +memset (global_cpumaps,0 ,CHANNEL_CMDS_MAX_CPUS * global_maplen); + ITERATIVE_BITMASK_CHECK_64(mask, i) { +VIR_USE_CPU (global_cpumaps, i); +if (i >= global_n_host_cpus) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"CPU(%u) exceeds the available " +"number of CPUs(%u)\n" , i, global_n_host_cpus); +return -1 ; + } + } +if (virDomainPinVcpuFlags (vm_info->domainPtr, vcpu, global_cpumaps, + global_maplen, flags) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to set vCPU(%u) to pCPU " +"mask(0x%" PRIx64") for VM '%s'\n" , vcpu, core_mask, + vm_info->name); +return -1 ; + } +rte_atomic64_set (&vm_info->pcpu_mask[vcpu], core_mask); +return 0 ; + +} + +int +set_pcpu (char *vm_name,unsigned vcpu,unsigned core_num) +{ +uint64_t mask =1ULL << core_num; + +return set_pcpus_mask (vm_name, vcpu, mask); +} +
examples/vm_power_manager/channel_manager.c line 235
Quoted 10 lines
examples/vm_power_manager/channel_manager.c new file +10
-0+uint64_t +get_pcpus_mask (struct channel_info *chan_info,unsigned vcpu) +{ +struct virtual_machine_info *vm_info = + (struct virtual_machine_info *)chan_info->priv_info; +return rte_atomic64_read (&vm_info->pcpu_mask[vcpu]); +} + +static inline int +channel_exists (struct virtual_machine_info *vm_info,unsigned channel_num)
Is your intent for this to always be inlined? If so, you likely meant to make this __always_inline__ (or whatever the unilateral inline macro is)
Quoted 1,245 lines
examples/vm_power_manager/channel_manager.c new file +573
-0+{ +rte_spinlock_lock (&(vm_info->config_spinlock)); +if (vm_info->channel_mask & (1ULL << channel_num)) { +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 1 ; + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 0 ; +} + + + +static int +open_non_blocking_channel (struct channel_info *info) +{ +int ret, flags; +struct sockaddr_un sock_addr; +fd_set soc_fd_set; +struct timeval tv; + + info->fd =socket (AF_UNIX ,SOCK_STREAM ,0 ); +if (info->fd ==-1 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error(%s) creating socket for '%s'\n" , +strerror (errno), + info->channel_path); +return -1 ; + } + sock_addr.sun_family =AF_UNIX ; +memcpy (&sock_addr.sun_path, info->channel_path, +strlen (info->channel_path)+1 ); + +/* Get current flags */ + flags =fcntl (info->fd,F_GETFL ,0 ); +if (flags <0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) fcntl get flags socket for" +"'%s'\n" ,strerror (errno), info->channel_path); +return 1 ; + } +/* Set to Non Blocking */ + flags |=O_NONBLOCK ; +if (fcntl (info->fd,F_SETFL , flags) <0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) setting non-blocking " +"socket for '%s'\n" ,strerror (errno), info->channel_path); +return -1 ; + } + ret =connect (info->fd, (struct sockaddr *)&sock_addr, +sizeof (sock_addr)); +if (ret <0 ) { +/* ECONNREFUSED error is given when VM is not active */ +if (errno ==ECONNREFUSED ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"VM is not active or has not " +"activated its endpoint to channel %s\n" , + info->channel_path); +return -1 ; + } +/* Wait for tv_sec if in progress */ +else if (errno ==EINPROGRESS ) { + tv.tv_sec =2 ; + tv.tv_usec =0 ; +FD_ZERO (&soc_fd_set); +FD_SET (info->fd, &soc_fd_set); +if (select (info->fd+1 ,NULL , &soc_fd_set,NULL , &tv) >0 ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Timeout or error on channel " +"'%s'\n" , info->channel_path); +return -1 ; + } + }else { +/* Any other error */ +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Error(%s) connecting socket" +" for '%s'\n" ,strerror (errno), info->channel_path); +return -1 ; + } + } +return 0 ; +} + +static int +setup_channel_info (struct virtual_machine_info **vm_info_dptr, +struct channel_info **chan_info_dptr,unsigned channel_num) +{ +struct channel_info *chan_info = *chan_info_dptr; +struct virtual_machine_info *vm_info = *vm_info_dptr; + + chan_info->channel_num = channel_num; + chan_info->priv_info = (void *)vm_info; + chan_info->status =CHANNEL_MGR_CHANNEL_DISCONNECTED ; +if (open_non_blocking_channel (chan_info) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Could not open channel: " +"'%s' for VM '%s'\n" , + chan_info->channel_path, vm_info->name); +return -1 ; + } +if (add_channel_to_monitor (&chan_info) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Could add channel: " +"'%s' to epoll ctl for VM '%s'\n" , + chan_info->channel_path, vm_info->name); +return -1 ; + + } +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->num_channels++; + vm_info->channel_mask |=1ULL << channel_num; + vm_info->channels[channel_num] = chan_info; + chan_info->status =CHANNEL_MGR_CHANNEL_CONNECTED ; +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return 0 ; +} + +int +add_all_channels (const char *vm_name) +{ +DIR *d; +struct dirent *dir; +struct virtual_machine_info *vm_info; +struct channel_info *chan_info; +char *token, *remaining, *tail_ptr; +char socket_name[PATH_MAX ]; +unsigned channel_num; +int num_channels_enabled =0 ; + +/* verify VM exists */ + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' not found" +" during channel discovery\n" , vm_name); +return 0 ; + } +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' is not active\n" , vm_name); + vm_info->status =CHANNEL_MGR_VM_INACTIVE ; +return 0 ; + } + d =opendir (CHANNEL_MGR_SOCKET_PATH ); +if (d ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error opening directory '%s': %s\n" , +CHANNEL_MGR_SOCKET_PATH ,strerror (errno)); +return -1 ; + } +while ((dir =readdir (d)) !=NULL ) { +if (!strncmp (dir->d_name,"." ,1 ) || + !strncmp (dir->d_name,".." ,2 )) +continue ; + +snprintf (socket_name,sizeof (socket_name),"%s" , dir->d_name); + remaining = socket_name; +/* Extract vm_name from "<vm_name>.<channel_num>" */ + token =strsep (&remaining,"." ); +if (remaining ==NULL ) +continue ; +if (strncmp (vm_name, token,CHANNEL_MGR_MAX_NAME_LEN )) +continue ; + +/* remaining should contain only <channel_num> */ + errno =0 ; + channel_num = (unsigned )strtol (remaining, &tail_ptr,0 ); +if ((errno !=0 ) || (remaining[0 ] =='\0' ) || + (*tail_ptr !='\0' ) || tail_ptr ==NULL ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Malformed channel name" +"'%s' found it should be in the form of " +"'<guest_name>.<channel_num>(decimal)'\n" , + dir->d_name); +continue ; + } +if (channel_num >=CHANNEL_CMDS_MAX_VM_CHANNELS ) { +RTE_LOG (WARNING ,CHANNEL_MANAGER ,"Channel number(%u) is " +"greater than max allowable: %d, skipping '%s%s'\n" , + channel_num,CHANNEL_CMDS_MAX_VM_CHANNELS -1 , +CHANNEL_MGR_SOCKET_PATH , dir->d_name); +continue ; + } +/* if channel has not been added previously */ +if (channel_exists (vm_info, channel_num)) +continue ; + + chan_info =rte_malloc (NULL ,sizeof (*chan_info), +CACHE_LINE_SIZE ); +if (chan_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for " +"channel '%s%s'\n" ,CHANNEL_MGR_SOCKET_PATH , dir->d_name); +continue ; + } + +snprintf (chan_info->channel_path, +sizeof (chan_info->channel_path),"%s%s" , +CHANNEL_MGR_SOCKET_PATH , dir->d_name); + +if (setup_channel_info (&vm_info, &chan_info, channel_num) <0 ) { +rte_free (chan_info); +continue ; + } + + num_channels_enabled++; + } +closedir (d); +return num_channels_enabled; +} + +int +add_channels (const char *vm_name,unsigned *channel_list, +unsigned len_channel_list) +{ +struct virtual_machine_info *vm_info; +struct channel_info *chan_info; +char socket_path[PATH_MAX ]; +unsigned i; +int num_channels_enabled =0 ; + + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } + +if (!virDomainIsActive (vm_info->domainPtr)) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM: '%s' is not active\n" , vm_name); + vm_info->status =CHANNEL_MGR_VM_INACTIVE ; +return 0 ; + } + +for (i =0 ; i < len_channel_list; i++) { + +if (channel_list[i] >=CHANNEL_CMDS_MAX_VM_CHANNELS ) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Channel(%u) is out of range " +"0...%d\n" , channel_list[i], +CHANNEL_CMDS_MAX_VM_CHANNELS -1 ); +continue ; + } +if (channel_exists (vm_info, channel_list[i])) { +RTE_LOG (INFO ,CHANNEL_MANAGER ,"Channel already exists, skipping " +"'%s.%u'\n" , vm_name, i); +continue ; + } + +snprintf (socket_path,sizeof (socket_path),"%s%s.%u" , +CHANNEL_MGR_SOCKET_PATH , vm_name, channel_list[i]); + errno =0 ; +if (access (socket_path,F_OK ) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channel path '%s' error: " +"%s\n" , socket_path,strerror (errno)); +continue ; + } + chan_info =rte_malloc (NULL ,sizeof (*chan_info), +CACHE_LINE_SIZE ); +if (chan_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for " +"channel '%s'\n" , socket_path); +continue ; + } +snprintf (chan_info->channel_path, +sizeof (chan_info->channel_path),"%s%s.%u" , +CHANNEL_MGR_SOCKET_PATH , vm_name, channel_list[i]); +if (setup_channel_info (&vm_info, &chan_info, channel_list[i]) <0 ) { +rte_free (chan_info); +continue ; + } + num_channels_enabled++; + + } +return num_channels_enabled; +} + +int +remove_channel (struct channel_info **chan_info_dptr) +{ +struct virtual_machine_info *vm_info; +struct channel_info *chan_info = *chan_info_dptr; + +close (chan_info->fd); + + vm_info = (struct virtual_machine_info *)chan_info->priv_info; + +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->channel_mask &= ~(1ULL << chan_info->channel_num); + vm_info->num_channels--; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +rte_free (chan_info); +return 0 ; +} + +int +set_channel_status_all (const char *vm_name,enum channel_status status) +{ +struct virtual_machine_info *vm_info; +unsigned i; +uint64_t mask; +int num_channels_changed =0 ; + +if (!(status ==CHANNEL_MGR_CHANNEL_CONNECTED || + status ==CHANNEL_MGR_CHANNEL_DISABLED )) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channels can only be enabled or " +"disabled: Unable to change status for VM '%s'\n" , vm_name); + } + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to disable channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } + +rte_spinlock_lock (&(vm_info->config_spinlock)); + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { + vm_info->channels[i]->status = status; + num_channels_changed++; + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); +return num_channels_changed; + +} + +int +set_channel_status (const char *vm_name,unsigned *channel_list, +unsigned len_channel_list,enum channel_status status) +{ +struct virtual_machine_info *vm_info; +unsigned i; +int num_channels_changed =0 ; + +if (!(status ==CHANNEL_MGR_CHANNEL_CONNECTED || + status ==CHANNEL_MGR_CHANNEL_DISABLED )) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Channels can only be enabled or " +"disabled: Unable to change status for VM '%s'\n" , vm_name); + } + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add channels: VM '%s' " +"not found\n" , vm_name); +return 0 ; + } +for (i =0 ; i < len_channel_list; i++) { +if (channel_exists (vm_info, channel_list[i])) { +rte_spinlock_lock (&(vm_info->config_spinlock)); + vm_info->channels[channel_list[i]]->status = status; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + num_channels_changed++; + } + } +return num_channels_changed; +} + +int +get_info_vm (const char *vm_name,struct vm_info *info) +{ +struct virtual_machine_info *vm_info; +unsigned i, channel_num =0 ; +uint64_t mask; + + vm_info =find_domain_by_name (vm_name); +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"VM '%s' not found\n" , vm_name); +return -1 ; + } + info->status =CHANNEL_MGR_VM_ACTIVE ; +if (!virDomainIsActive (vm_info->domainPtr)) + info->status =CHANNEL_MGR_VM_INACTIVE ; + +rte_spinlock_lock (&(vm_info->config_spinlock)); + + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { + info->channels[channel_num].channel_num = i; +memcpy (info->channels[channel_num].channel_path, + vm_info->channels[i]->channel_path,PATH_MAX ); + info->channels[channel_num].status = vm_info->channels[i]->status; + info->channels[channel_num].fd = vm_info->channels[i]->fd; + channel_num++; + } + + info->num_channels = channel_num; + info->num_vcpus = vm_info->info.nrVirtCpu; +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +memcpy (info->name, vm_info->name,sizeof (vm_info->name)); +for (i =0 ; i < info->num_vcpus; i++) { + info->pcpu_mask[i] =rte_atomic64_read (&vm_info->pcpu_mask[i]); + } +return 0 ; +} + +int +add_vm (const char *vm_name) +{ +struct virtual_machine_info *new_domain; +virDomainPtr dom_ptr; +int i; + +if (find_domain_by_name (vm_name) !=NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to add VM: VM '%s' " +"already exists\n" , vm_name); +return -1 ; + } + +if (global_vir_conn_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"No connection to hypervisor exists\n" ); +return -1 ; + } + dom_ptr =virDomainLookupByName (global_vir_conn_ptr, vm_name); +if (dom_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error on VM lookup with libvirt: " +"VM '%s' not found\n" , vm_name); +return -1 ; + } + + new_domain =rte_malloc ("virtual_machine_info" ,sizeof (*new_domain), +CACHE_LINE_SIZE ); +if (new_domain ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to allocate memory for VM " +"info\n" ); +return -1 ; + } + new_domain->domainPtr = dom_ptr; +if (virDomainGetInfo (new_domain->domainPtr, &new_domain->info) !=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to get libvirt VM info\n" ); +rte_free (new_domain); +return -1 ; + } +if (new_domain->info.nrVirtCpu >CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error the number of virtual CPUs(%u) is " +"greater than allowable(%d)\n" , new_domain->info.nrVirtCpu, +CHANNEL_CMDS_MAX_CPUS ); +rte_free (new_domain); +return -1 ; + } + +for (i =0 ; i <CHANNEL_CMDS_MAX_CPUS ; i++) { +rte_atomic64_init (&new_domain->pcpu_mask[i]); + } +if (update_pcpus_mask (new_domain) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error getting physical CPU pinning\n" ); +rte_free (new_domain); +return -1 ; + } +strncpy (new_domain->name, vm_name,sizeof (new_domain->name)); + new_domain->channel_mask =0 ; + new_domain->num_channels =0 ; + +if (!virDomainIsActive (dom_ptr)) + new_domain->status =CHANNEL_MGR_VM_INACTIVE ; +else + new_domain->status =CHANNEL_MGR_VM_ACTIVE ; + +rte_spinlock_init (&(new_domain->config_spinlock)); +LIST_INSERT_HEAD (&vm_list_head, new_domain, vms_info); +return 0 ; +} + +int +remove_vm (const char *vm_name) +{ +struct virtual_machine_info *vm_info =find_domain_by_name (vm_name); + +if (vm_info ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to remove VM: VM '%s' " +"not found\n" , vm_name); +return -1 ; + } +rte_spinlock_lock (&vm_info->config_spinlock); +if (vm_info->num_channels !=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to remove VM '%s', there are " +"%" PRId8" channels still active\n" , + vm_name, vm_info->num_channels); +rte_spinlock_unlock (&vm_info->config_spinlock); +return -1 ; + } +LIST_REMOVE (vm_info, vms_info); +rte_spinlock_unlock (&vm_info->config_spinlock); +rte_free (vm_info); +return 0 ; +} + +static void +disconnect_hypervisor (void ) +{ +if (global_vir_conn_ptr !=NULL ) { +virConnectClose (global_vir_conn_ptr); + global_vir_conn_ptr =NULL ; + } +} + +static int +connect_hypervisor (const char *path) +{ +if (global_vir_conn_ptr !=NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error connecting to %s, connection" +"already established\n" , path); +return -1 ; + } + global_vir_conn_ptr =virConnectOpen (path); +if (global_vir_conn_ptr ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error failed to open connection to " +"Hypervisor '%s'\n" , path); +return -1 ; + } +return 0 ; +} + +int +channel_manager_init (const char *path) +{ +int n_cpus; + +LIST_INIT (&vm_list_head); +if (connect_hypervisor (path) <0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to initialize channel manager\n" ); +return -1 ; + } + + global_maplen =VIR_CPU_MAPLEN (CHANNEL_CMDS_MAX_CPUS ); + + global_vircpuinfo =rte_zmalloc (NULL ,sizeof (*global_vircpuinfo) * +CHANNEL_CMDS_MAX_CPUS ,CACHE_LINE_SIZE ); +if (global_vircpuinfo ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Error allocating memory for CPU Info\n" ); + goto error; + } + global_cpumaps =rte_zmalloc (NULL ,CHANNEL_CMDS_MAX_CPUS * global_maplen, +CACHE_LINE_SIZE ); +if (global_cpumaps ==NULL ) { + goto error; + } + + n_cpus =virNodeGetCPUMap (global_vir_conn_ptr,NULL ,NULL ,0 ); +if (n_cpus <=0 ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"Unable to get the number of Host " +"CPUs\n" ); + goto error; + } + global_n_host_cpus = (unsigned )n_cpus; + +if (global_n_host_cpus >CHANNEL_CMDS_MAX_CPUS ) { +RTE_LOG (ERR ,CHANNEL_MANAGER ,"The number of host CPUs(%u) exceeds the " +"maximum of %u\n" , global_n_host_cpus,CHANNEL_CMDS_MAX_CPUS ); + goto error; + + } + +return 0 ; +error: +disconnect_hypervisor (); +return -1 ; +} + +void +channel_manager_exit (void ) +{ +unsigned i; +uint64_t mask; +struct virtual_machine_info *vm_info; + + LIST_FOREACH(vm_info, &vm_list_head, vms_info) { + +rte_spinlock_lock (&(vm_info->config_spinlock)); + + mask = vm_info->channel_mask; + ITERATIVE_BITMASK_CHECK_64(mask, i) { +remove_channel_from_monitor (vm_info->channels[i]); +close (vm_info->channels[i]->fd); +rte_free (vm_info->channels[i]); + } +rte_spinlock_unlock (&(vm_info->config_spinlock)); + +LIST_REMOVE (vm_info, vms_info); +rte_free (vm_info); + } + +if (global_cpumaps !=NULL ) +rte_free (global_cpumaps); +if (global_vircpuinfo !=NULL ) +rte_free (global_vircpuinfo); +disconnect_hypervisor (); +}examples/vm_power_manager/channel_manager.h new file +314
-0@@ -0,0 +1,314 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef CHANNEL_MANAGER_H_ +#define CHANNEL_MANAGER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <linux/limits.h> +#include <rte_atomic.h> +#include "channel_commands.h" + +/* Maximum name length including '\0' terminator */ +#define CHANNEL_MGR_MAX_NAME_LEN 64 + +/* Maximum number of channels to each Virtual Machine */ +#define CHANNEL_MGR_MAX_CHANNELS 64 + +/* Hypervisor Path for libvirt(qemu/KVM) */ +#define CHANNEL_MGR_DEFAULT_HV_PATH "qemu:///system" + +/* File socket directory */ +#define CHANNEL_MGR_SOCKET_PATH "/tmp/powermonitor/" + +/* Communication Channel Status */ +enum channel_status {CHANNEL_MGR_CHANNEL_DISCONNECTED =0 , +CHANNEL_MGR_CHANNEL_CONNECTED , +CHANNEL_MGR_CHANNEL_DISABLED , +CHANNEL_MGR_CHANNEL_PROCESSING }; + +/* VM libvirt(qemu/KVM) connection status */ +enum vm_status {CHANNEL_MGR_VM_INACTIVE =0 ,CHANNEL_MGR_VM_ACTIVE }; + +/* +* Represents a single and exclusive VM channel that exists between a guest and +* the host. +*/ +struct channel_info { +char channel_path[PATH_MAX ];/**< Path to host socket */ +volatile uint32_t status;/**< Connection status(enum channel_status) */ +int fd;/**< AF_UNIX socket fd */ +unsigned channel_num;/**< CHANNEL_MGR_SOCKET_PATH/<vm_name>.channel_num */ +void *priv_info;/**< Pointer to private info, do not modify */ +}; + +/* Represents a single VM instance used to return internal information about +* a VM */ +struct vm_info { +char name[CHANNEL_MGR_MAX_NAME_LEN ];/**< VM name */ +enum vm_status status;/**< libvirt status */ +uint64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS ];/**< pCPU mask for each vCPU */ +unsigned num_vcpus;/**< number of vCPUS */ +struct channel_info channels[CHANNEL_MGR_MAX_CHANNELS ];/**< Array of channel_info */ +unsigned num_channels;/**< Number of channels */ +}; + +/** +* Initialize the Channel Manager resources and connect to the Hypervisor +* specified in path. +* This must be successfully called first before calling any other functions. +* It must only be call once; +* +* @param path +* Must be a local path, e.g. qemu:///system. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int channel_manager_init (const char *path); + +/** +* Free resources associated with the Channel Manager. +* +* @param path +* Must be a local path, e.g. qemu:///system. +* +* @return +* None +*/ +void channel_manager_exit (void ); + +/** +* Get the Physical CPU mask for VM lcore channel(vcpu), result is assigned to +* core_mask. +* It is not thread-safe. +* +* @param chan_info +* Pointer to struct channel_info +* +* @param vcpu +* The virtual CPU to query. +* +* +* @return +* - 0 on error. +* - >0 on success. +*/ +uint64_t get_pcpus_mask (struct channel_info *chan_info,unsigned vcpu); + +/** +* Set the Physical CPU mask for the specified vCPU. +* It is not thread-safe. +* +* @param name +* Virtual Machine name to lookup +* +* @param vcpu +* The virtual CPU to set. +* +* @param core_mask +* The core mask of the physical CPU(s) to bind the vCPU +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int set_pcpus_mask (char *vm_name,unsigned vcpu,uint64_t core_mask); + +/** +* Set the Physical CPU for the specified vCPU. +* It is not thread-safe. +* +* @param name +* Virtual Machine name to lookup +* +* @param vcpu +* The virtual CPU to set. +* +* @param core_num +* The core number of the physical CPU(s) to bind the vCPU +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int set_pcpu (char *vm_name,unsigned vcpu,unsigned core_num); +/** +* Add a VM as specified by name to the Channel Manager. The name must +* correspond to a valid libvirt domain name. +* This is required prior to adding channels. +* It is not thread-safe. +* +* @param name +* Virtual Machine name to lookup. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int add_vm (const char *name); + +/** +* Remove a previously added Virtual Machine from the Channel Manager +* It is not thread-safe. +* +* @param name +* Virtual Machine name to lookup. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int remove_vm (const char *name); + +/** +* Add all available channels to the VM as specified by name. +* Channels in the form of paths +* (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed. +* It is not thread-safe. +* +* @param name +* Virtual Machine name to lookup. +* +* @return +* - N the number of channels added for the VM +*/ +int add_all_channels (const char *vm_name); + +/** +* Add the channel numbers in channel_list to the domain specified by name. +* Channels in the form of paths +* (CHANNEL_MGR_SOCKET_PATH/<vm_name>.<channel_number>) will only be parsed. +* It is not thread-safe. +* +* @param name +* Virtual Machine name to add channels. +* +* @param channel_list +* Pointer to list of unsigned integers, representing the channel number to add +* It must be allocated outside of this function. +* +* @param num_channels +* The amount of channel numbers in channel_list +* +* @return +* - N the number of channels added for the VM +* - 0 for error +*/ +int add_channels (const char *vm_name,unsigned *channel_list, +unsigned num_channels); + +/** +* Remove a channel definition from the channel manager. This must only be +* called from the channel monitor thread. +* +* @param chan_info +* Pointer to a valid struct channel_info. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int remove_channel (struct channel_info **chan_info_dptr); + +/** +* For all channels associated with a Virtual Machine name, update the +* connection status. Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or +* CHANNEL_MGR_CHANNEL_DISABLED only. +* +* +* @param name +* Virtual Machine name to modify all channels. +* +* @param status +* The status to set each channel +* +* @param num_channels +* The amount of channel numbers in channel_list +* +* @return +* - N the number of channels added for the VM +* - 0 for error +*/ +int set_channel_status_all (const char *name,enum channel_status status); + +/** +* For all channels in channel_list associated with a Virtual Machine name +* update the connection status of each. +* Valid states are CHANNEL_MGR_CHANNEL_CONNECTED or +* CHANNEL_MGR_CHANNEL_DISABLED only. +* It is not thread-safe. +* +* @param name +* Virtual Machine name to add channels. +* +* @param channel_list +* Pointer to list of unsigned integers, representing the channel numbers to +* modify. +* It must be allocated outside of this function. +* +* @param num_channels +* The amount of channel numbers in channel_list +* +* @return +* - N the number of channels modified for the VM +* - 0 for error +*/ +int set_channel_status (const char *vm_name,unsigned *channel_list, +unsigned len_channel_list,enum channel_status status); + +/** +* Populates a pointer to struct vm_info associated with vm_name. +* +* @param vm_name +* The name of the virtual machine to lookup. +* +* @param vm_info +* Pointer to a struct vm_info, this must be allocated prior to calling this +* function. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int get_info_vm (const char *vm_name,struct vm_info *info); + +#ifdef __cplusplus +} +#endif + +#endif /* CHANNEL_MANAGER_H_ */ examples/vm_power_manager/channel_monitor.c new file +234
-0@@ -0,0 +1,234 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <signal.h> +#include <errno.h> +#include <string.h> +#include <sys/types.h> +#include <sys/epoll.h> +#include <sys/queue.h> + +#include <rte_config.h> +#include <rte_log.h> +#include <rte_memory.h> +#include <rte_malloc.h> +#include <rte_atomic.h> + + +#include "channel_monitor.h" +#include "channel_commands.h" +#include "channel_manager.h" +#include "power_manager.h" + +#define RTE_LOGTYPE_CHANNEL_MONITOR RTE_LOGTYPE_USER1 + +#define MAX_EVENTS 256 + + +static volatile unsigned run_loop =1 ; +static int global_event_fd; +static struct epoll_event *global_events_list; + +void channel_monitor_exit (void ) +{ + run_loop =0 ; +rte_free (global_events_list); +} + +static int +process_request (struct channel_packet *pkt,struct channel_info *chan_info) +{ +uint64_t core_mask; + +if (chan_info ==NULL ) +return -1 ; + +if (rte_atomic32_cmpset (&(chan_info->status),CHANNEL_MGR_CHANNEL_CONNECTED , +CHANNEL_MGR_CHANNEL_PROCESSING ) ==0 ) +return -1 ; + +if (pkt->command ==CPU_POWER ) { + core_mask =get_pcpus_mask (chan_info, pkt->resource_id); +if (core_mask ==0 ) { +RTE_LOG (ERR ,CHANNEL_MONITOR ,"Error get physical CPU mask for " +"channel '%s' using vCPU(%u)\n" , chan_info->channel_path, + (unsigned )pkt->unit); +return -1 ; + } +if (__builtin_popcountll (core_mask) ==1 ) { + +unsigned core_num =__builtin_ffsll (core_mask) -1 ; + +switch (pkt->unit) { +case (CPU_POWER_SCALE_MIN ): +power_manager_scale_core_min (core_num); +break ; +case (CPU_POWER_SCALE_MAX ): +power_manager_scale_core_max (core_num); +break ; +case (CPU_POWER_SCALE_DOWN ): +power_manager_scale_core_down (core_num); +break ; +case (CPU_POWER_SCALE_UP ): +power_manager_scale_core_up (core_num); +break ; +default : +break ; + } + }else { +switch (pkt->unit) { +case (CPU_POWER_SCALE_MIN ): +power_manager_scale_mask_min (core_mask); +break ; +case (CPU_POWER_SCALE_MAX ): +power_manager_scale_mask_max (core_mask); +break ; +case (CPU_POWER_SCALE_DOWN ): +power_manager_scale_mask_down (core_mask); +break ; +case (CPU_POWER_SCALE_UP ): +power_manager_scale_mask_up (core_mask); +break ; +default : +break ; + } + + } + } +/* Return is not checked as channel status may have been set to DISABLED +* from management thread +*/ +rte_atomic32_cmpset (&(chan_info->status),CHANNEL_MGR_CHANNEL_PROCESSING , +CHANNEL_MGR_CHANNEL_CONNECTED ); +return 0 ; + +} + +int +add_channel_to_monitor (struct channel_info **chan_info) +{ +struct channel_info *info = *chan_info; +struct epoll_event event; + + event.events =EPOLLIN ; + event.data.ptr = info; +if (epoll_ctl (global_event_fd,EPOLL_CTL_ADD , info->fd, &event) <0 ) { +RTE_LOG (ERR ,CHANNEL_MONITOR ,"Unable to add channel '%s' " +"to epoll\n" , info->channel_path); +return -1 ; + } +return 0 ; +} + +int +remove_channel_from_monitor (struct channel_info *chan_info) +{ +if (epoll_ctl (global_event_fd,EPOLL_CTL_DEL , chan_info->fd,NULL ) <0 ) { +RTE_LOG (ERR ,CHANNEL_MONITOR ,"Unable to remove channel '%s' " +"from epoll\n" , chan_info->channel_path); +return -1 ; + } +return 0 ; +} + +int +channel_monitor_init (void ) +{ + global_event_fd =epoll_create1 (0 ); +if (global_event_fd ==0 ) { +RTE_LOG (ERR ,CHANNEL_MONITOR ,"Error creating epoll context with " +"error %s\n" ,strerror (errno)); +return -1 ; + } + global_events_list =rte_malloc ("epoll_events" ,sizeof (*global_events_list) + *MAX_EVENTS ,CACHE_LINE_SIZE ); +if (global_events_list ==NULL ) { +RTE_LOG (ERR ,CHANNEL_MONITOR ,"Unable to rte_malloc for" +"epoll events\n" ); +return -1 ; + } +return 0 ; +} + +void +run_channel_monitor (void ) +{ +while (run_loop) { +int n_events, i; + + n_events =epoll_wait (global_event_fd, global_events_list, +MAX_EVENTS ,1 ); +if (!run_loop) +break ; +for (i =0 ; i < n_events; i++) { +struct channel_info *chan_info = (struct channel_info *) + global_events_list[i].data.ptr; +if ((global_events_list[i].events &EPOLLERR ) || + (global_events_list[i].events &EPOLLHUP )) { +RTE_LOG (DEBUG ,CHANNEL_MONITOR ,"Remote closed connection for " +"channel '%s'\n" , chan_info->channel_path); +remove_channel (&chan_info); +continue ; + } +if (global_events_list[i].events &EPOLLIN ) { + +int n_bytes, err =0 ; +struct channel_packet pkt; +void *buffer = &pkt; +int buffer_len =sizeof (pkt); + +while (buffer_len >0 ) { + n_bytes =read (chan_info->fd, buffer, buffer_len); +if (n_bytes == buffer_len) +break ; +if (n_bytes ==-1 ) { + err = errno; +RTE_LOG (DEBUG ,CHANNEL_MONITOR ,"Received error on " +"channel '%s' read: %s\n" , + chan_info->channel_path,strerror (err)); +remove_channel (&chan_info); +break ; + } + buffer = (char *)buffer + n_bytes; + buffer_len -= n_bytes; + } +if (!err) +process_request (&pkt, chan_info); + } + } + } +}examples/vm_power_manager/channel_monitor.h new file +102
-0@@ -0,0 +1,102 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef CHANNEL_MONITOR_H_ +#define CHANNEL_MONITOR_H_ + +#include "channel_manager.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Setup the Channel Monitor resources required to initialize epoll. +* Must be called first before calling other functions. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int channel_monitor_init (void ); + +/** +* Run the channel monitor, loops forever on on epoll_wait. +* +* +* @return +* None +*/ +void run_channel_monitor (void ); + +/** +* Exit the Channel Monitor, exiting the epoll_wait loop and events processing. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +void channel_monitor_exit (void ); + +/** +* Add an open channel to monitor via epoll. A pointer to struct channel_info +* will be registered with epoll for event processing. +* It is thread-safe. +* +* @param chan_info +* Pointer to struct channel_info pointer. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int add_channel_to_monitor (struct channel_info **chan_info); + +/** +* Remove a previously added channel from epoll control. +* +* @param chan_info +* Pointer to struct channel_info. +* +* @return +* - 0 on success. +* - Negative on error. +*/ +int remove_channel_from_monitor (struct channel_info *chan_info); + +#ifdef __cplusplus +} +#endif + + +#endif /* CHANNEL_MONITOR_H_ */ Signature · 1 line
1.7.4.1
PATCH v6 2/10 VM Power Management CLI(Host).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
The CLI is used for administrating the channel monitor and manager and manually setting the CPU frequency on the host.
Supports the following commands:
add_vm [Mul-choice STRING]: add_vm|rm_vm <name>, add a VM for subsequent
operations with the CLI or remove a previously added VM from the VM Power Manager
rm_vm [Mul-choice STRING]: add_vm|rm_vm <name>, add a VM for subsequent
operations with the CLI or remove a previously added VM from the VM Power Manager
add_channels [Fixed STRING]: add_channels <vm_name> <list>|all, add
communication channels for the specified VM, the virtio channels must be enabled in the VM configuration(qemu/libvirt) and the associated VM must be active. <list> is a comma-separated list of channel numbers to add, using the keyword 'all' will attempt to add all channels for the VM
set_channel_status [Fixed STRING]:
set_channel_status <vm_name> <list>|all enabled|disabled, enable or disable the communication channels in list(comma-separated) for the specified VM, alternatively list can be replaced with keyword 'all'. Disabled channels will still receive packets on the host, however the commands they specify will be ignored. Set status to 'enabled' to begin processing requests again.
show_vm [Fixed STRING]: show_vm <vm_name>, prints the information on the
specified VM(s), the information lists the number of vCPUS, the pinning to pCPU(s) as a bit mask, along with any communication channels associated with each VM
show_cpu_freq_mask [Fixed STRING]: show_cpu_freq_mask <mask>, Get the current
frequency for each core specified in the mask
set_cpu_freq_mask [Fixed STRING]: set_cpu_freq <core_mask> <up|down|min|max>,
Set the current frequency for the cores specified in <core_mask> by scaling each up/down/min/max.
show_cpu_freq [Fixed STRING]: Get the current frequency for the specified core
set_cpu_freq [Fixed STRING]: set_cpu_freq <core_num> <up|down|min|max>,
Set the current frequency for the specified core by scaling up/down/min/max
quit [Fixed STRING]: close the application
--- examples/vm_power_manager/vm_power_cli.c | 673 ++++++++++++++++++++++++++++++ examples/vm_power_manager/vm_power_cli.h | 47 ++ 2 files changed, 720 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/vm_power_cli.c create mode 100644 examples/vm_power_manager/vm_power_cli.h
examples/vm_power_manager/vm_power_cli.c new file +673 -0
@@ -0,0 +1,673 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdlib.h> +#include <stdint.h> +#include <inttypes.h> +#include <stdio.h> +#include <string.h> +#include <termios.h> +#include <errno.h> + +#include <cmdline_rdline.h> +#include <cmdline_parse.h> +#include <cmdline_parse_string.h> +#include <cmdline_parse_num.h> +#include <cmdline_socket.h> +#include <cmdline.h> +#include <rte_config.h> + +#include "vm_power_cli.h" +#include "channel_manager.h" +#include "channel_monitor.h" +#include "power_manager.h" +#include "channel_commands.h" + +struct cmd_quit_result { +cmdline_fixed_string_t quit; +}; + +static void cmd_quit_parsed (__attribute__((unused))void *parsed_result, +struct cmdline *cl, + __attribute__((unused))void *data) +{ +channel_monitor_exit (); +channel_manager_exit (); +power_manager_exit (); +cmdline_quit (cl); +} + +cmdline_parse_token_string_t cmd_quit_quit = + TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit"); + +cmdline_parse_inst_t cmd_quit = { + .f = cmd_quit_parsed,/* function to call */ + .data =NULL ,/* 2nd arg of func */ + .help_str ="close the application" , + .tokens = {/* token list, NULL terminated */ + (void *)&cmd_quit_quit, +NULL , + }, +}; + +/* *** VM operations *** */ +struct cmd_show_vm_result { +cmdline_fixed_string_t show_vm; +cmdline_fixed_string_t vm_name; +}; + +static void +cmd_show_vm_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_show_vm_result *res = parsed_result; +struct vm_info info; +unsigned i; + +if (get_info_vm (res->vm_name, &info) !=0 ) +return ; +cmdline_printf (cl,"VM: '%s', status = " , info.name); +if (info.status ==CHANNEL_MGR_VM_ACTIVE ) +cmdline_printf (cl,"ACTIVE\n" ); +else +cmdline_printf (cl,"INACTIVE\n" ); +cmdline_printf (cl,"Channels %u\n" , info.num_channels); +for (i =0 ; i < info.num_channels; i++) { +cmdline_printf (cl," [%u]: %s, status = " , i, + info.channels[i].channel_path); +switch (info.channels[i].status) { +case CHANNEL_MGR_CHANNEL_CONNECTED : +cmdline_printf (cl,"CONNECTED\n" ); +break ; +case CHANNEL_MGR_CHANNEL_DISCONNECTED : +cmdline_printf (cl,"DISCONNECTED\n" ); +break ; +case CHANNEL_MGR_CHANNEL_DISABLED : +cmdline_printf (cl,"DISABLED\n" ); +break ; +case CHANNEL_MGR_CHANNEL_PROCESSING : +cmdline_printf (cl,"PROCESSING\n" ); +break ; +default : +cmdline_printf (cl,"UNKNOWN\n" ); +break ; + } + } +cmdline_printf (cl,"Virtual CPU(s): %u\n" , info.num_vcpus); +for (i =0 ; i < info.num_vcpus; i++) { +cmdline_printf (cl," [%u]: Physical CPU Mask 0x%" PRIx64"\n" , i, + info.pcpu_mask[i]); + } +} + + + +cmdline_parse_token_string_t cmd_vm_show = + TOKEN_STRING_INITIALIZER(struct cmd_show_vm_result, + show_vm,"show_vm" ); +cmdline_parse_token_string_t cmd_show_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_show_vm_result, + vm_name,NULL ); + +cmdline_parse_inst_t cmd_show_vm_set = { + .f = cmd_show_vm_parsed, + .data =NULL , + .help_str ="show_vm <vm_name>, prints the information on the " +"specified VM(s), the information lists the number of vCPUS, the " +"pinning to pCPU(s) as a bit mask, along with any communication " +"channels associated with each VM" , + .tokens = { + (void *)&cmd_vm_show, + (void *)&cmd_show_vm_name, +NULL , + }, +}; + +/* *** vCPU to pCPU mapping operations *** */ +struct cmd_set_pcpu_mask_result { +cmdline_fixed_string_t set_pcpu_mask; +cmdline_fixed_string_t vm_name; +uint8_t vcpu; +uint64_t core_mask; +}; + +static void +cmd_set_pcpu_mask_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_set_pcpu_mask_result *res = parsed_result; + +if (set_pcpus_mask (res->vm_name, res->vcpu, res->core_mask) ==0 ) +cmdline_printf (cl,"Pinned vCPU(%" PRId8") to pCPU core " +"mask(0x%" PRIx64")\n" , res->vcpu, res->core_mask); +else +cmdline_printf (cl,"Unable to pin vCPU(%" PRId8") to pCPU core " +"mask(0x%" PRIx64")\n" , res->vcpu, res->core_mask); +} + +cmdline_parse_token_string_t cmd_set_pcpu_mask = + TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_mask_result, + set_pcpu_mask,"set_pcpu_mask" ); +cmdline_parse_token_string_t cmd_set_pcpu_mask_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_mask_result, + vm_name,NULL ); +cmdline_parse_token_num_t set_pcpu_mask_vcpu = + TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_mask_result, + vcpu,UINT8 ); +cmdline_parse_token_num_t set_pcpu_mask_core_mask = + TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_mask_result, + core_mask,UINT64 ); + + +cmdline_parse_inst_t cmd_set_pcpu_mask_set = { + .f = cmd_set_pcpu_mask_parsed, + .data =NULL , + .help_str ="set_pcpu_mask <vm_name> <vcpu> <pcpu>, Set the binding " +"of Virtual CPU on VM to the Physical CPU mask." , + .tokens = { + (void *)&cmd_set_pcpu_mask, + (void *)&cmd_set_pcpu_mask_vm_name, + (void *)&set_pcpu_mask_vcpu, + (void *)&set_pcpu_mask_core_mask, +NULL , + }, +}; + +struct cmd_set_pcpu_result { +cmdline_fixed_string_t set_pcpu; +cmdline_fixed_string_t vm_name; +uint8_t vcpu; +uint8_t core; +}; + +static void +cmd_set_pcpu_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_set_pcpu_result *res = parsed_result; + +if (set_pcpu (res->vm_name, res->vcpu, res->core) ==0 ) +cmdline_printf (cl,"Pinned vCPU(%" PRId8") to pCPU core " +"%" PRId8")\n" , res->vcpu, res->core); +else +cmdline_printf (cl,"Unable to pin vCPU(%" PRId8") to pCPU core " +"%" PRId8")\n" , res->vcpu, res->core); +} + +cmdline_parse_token_string_t cmd_set_pcpu = + TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_result, + set_pcpu,"set_pcpu" ); +cmdline_parse_token_string_t cmd_set_pcpu_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_set_pcpu_result, + vm_name,NULL ); +cmdline_parse_token_num_t set_pcpu_vcpu = + TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_result, + vcpu,UINT8 ); +cmdline_parse_token_num_t set_pcpu_core = + TOKEN_NUM_INITIALIZER(struct cmd_set_pcpu_result, + core,UINT64 ); + + +cmdline_parse_inst_t cmd_set_pcpu_set = { + .f = cmd_set_pcpu_parsed, + .data =NULL , + .help_str ="set_pcpu <vm_name> <vcpu> <pcpu>, Set the binding " +"of Virtual CPU on VM to the Physical CPU." , + .tokens = { + (void *)&cmd_set_pcpu, + (void *)&cmd_set_pcpu_vm_name, + (void *)&set_pcpu_vcpu, + (void *)&set_pcpu_core, +NULL , + }, +}; + +struct cmd_vm_op_result { +cmdline_fixed_string_t op_vm; +cmdline_fixed_string_t vm_name; +}; + +static void +cmd_vm_op_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_vm_op_result *res = parsed_result; + +if (!strcmp (res->op_vm,"add_vm" )) { +if (add_vm (res->vm_name) <0 ) +cmdline_printf (cl,"Unable to add VM '%s'\n" , res->vm_name); + }else if (remove_vm (res->vm_name) <0 ) +cmdline_printf (cl,"Unable to remove VM '%s'\n" , res->vm_name); +} + +cmdline_parse_token_string_t cmd_vm_op = + TOKEN_STRING_INITIALIZER(struct cmd_vm_op_result, + op_vm,"add_vm#rm_vm" ); +cmdline_parse_token_string_t cmd_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_vm_op_result, + vm_name,NULL ); + +cmdline_parse_inst_t cmd_vm_op_set = { + .f = cmd_vm_op_parsed, + .data =NULL , + .help_str ="add_vm|rm_vm <name>, add a VM for " +"subsequent operations with the CLI or remove a previously added " +"VM from the VM Power Manager" , + .tokens = { + (void *)&cmd_vm_op, + (void *)&cmd_vm_name, +NULL , + }, +}; + +/* *** VM channel operations *** */ +struct cmd_channels_op_result { +cmdline_fixed_string_t op; +cmdline_fixed_string_t vm_name; +cmdline_fixed_string_t channel_list; +}; +static void +cmd_channels_op_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +unsigned num_channels =0 , channel_num, i; +int channels_added; +unsigned channel_list[CHANNEL_CMDS_MAX_VM_CHANNELS ]; +char *token, *remaining, *tail_ptr; +struct cmd_channels_op_result *res = parsed_result; + +if (!strcmp (res->channel_list,"all" )) { + channels_added =add_all_channels (res->vm_name); +cmdline_printf (cl,"Added %d channels for VM '%s'\n" , + channels_added, res->vm_name); +return ; + } + + remaining = res->channel_list; +while (1 ) { +if (remaining ==NULL || remaining[0 ] =='\0' ) +break ; + + token =strsep (&remaining,"," ); +if (token ==NULL ) +break ; + errno =0 ; + channel_num = (unsigned )strtol (token, &tail_ptr,10 ); +if ((errno !=0 ) || (*tail_ptr !='\0' ) || tail_ptr ==NULL ) +break ; + +if (channel_num ==CHANNEL_CMDS_MAX_VM_CHANNELS ) { +cmdline_printf (cl,"Channel number '%u' exceeds the maximum number " +"of allowable channels(%u) for VM '%s'\n" , channel_num, +CHANNEL_CMDS_MAX_VM_CHANNELS , res->vm_name); +return ; + } + channel_list[num_channels++] = channel_num; + } +for (i =0 ; i < num_channels; i++) +cmdline_printf (cl,"[%u]: Adding channel %u\n" , i, channel_list[i]); + + channels_added =add_channels (res->vm_name, channel_list, + num_channels); +cmdline_printf (cl,"Enabled %d channels for '%s'\n" , channels_added, + res->vm_name); +} + +cmdline_parse_token_string_t cmd_channels_op = + TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result, + op,"add_channels" ); +cmdline_parse_token_string_t cmd_channels_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result, + vm_name,NULL ); +cmdline_parse_token_string_t cmd_channels_list = + TOKEN_STRING_INITIALIZER(struct cmd_channels_op_result, + channel_list,NULL ); + +cmdline_parse_inst_t cmd_channels_op_set = { + .f = cmd_channels_op_parsed, + .data =NULL , + .help_str ="add_channels <vm_name> <list>|all, add " +"communication channels for the specified VM, the " +"virtio channels must be enabled in the VM " +"configuration(qemu/libvirt) and the associated VM must be active. " +"<list> is a comma-separated list of channel numbers to add, using " +"the keyword 'all' will attempt to add all channels for the VM" , + .tokens = { + (void *)&cmd_channels_op, + (void *)&cmd_channels_vm_name, + (void *)&cmd_channels_list, +NULL , + }, +}; + +struct cmd_channels_status_op_result { +cmdline_fixed_string_t op; +cmdline_fixed_string_t vm_name; +cmdline_fixed_string_t channel_list; +cmdline_fixed_string_t status; +}; + +static void +cmd_channels_status_op_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +unsigned num_channels =0 , channel_num; +int changed; +unsigned channel_list[CHANNEL_CMDS_MAX_VM_CHANNELS ]; +char *token, *remaining, *tail_ptr; +struct cmd_channels_status_op_result *res = parsed_result; +enum channel_status status; + +if (!strcmp (res->status,"enabled" )) + status =CHANNEL_MGR_CHANNEL_CONNECTED ; +else + status =CHANNEL_MGR_CHANNEL_DISABLED ; + +if (!strcmp (res->channel_list,"all" )) { + changed =set_channel_status_all (res->vm_name, status); +cmdline_printf (cl,"Updated status of %d channels " +"for VM '%s'\n" , changed, res->vm_name); +return ; + } + remaining = res->channel_list; +while (1 ) { +if (remaining ==NULL || remaining[0 ] =='\0' ) +break ; + token =strsep (&remaining,"," ); +if (token ==NULL ) +break ; + errno =0 ; + channel_num = (unsigned )strtol (token, &tail_ptr,10 ); +if ((errno !=0 ) || (*tail_ptr !='\0' ) || tail_ptr ==NULL ) +break ; + +if (channel_num ==CHANNEL_CMDS_MAX_VM_CHANNELS ) { +cmdline_printf (cl,"%u exceeds the maximum number of allowable " +"channels(%u) for VM '%s'\n" , channel_num, +CHANNEL_CMDS_MAX_VM_CHANNELS , res->vm_name); +return ; + } + channel_list[num_channels++] = channel_num; + } + changed =set_channel_status (res->vm_name, channel_list, num_channels, + status); +cmdline_printf (cl,"Updated status of %d channels " +"for VM '%s'\n" , changed, res->vm_name); +} + +cmdline_parse_token_string_t cmd_channels_status_op = + TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result, + op,"set_channel_status" ); +cmdline_parse_token_string_t cmd_channels_status_vm_name = + TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result, + vm_name,NULL ); +cmdline_parse_token_string_t cmd_channels_status_list = + TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result, + channel_list,NULL ); +cmdline_parse_token_string_t cmd_channels_status = + TOKEN_STRING_INITIALIZER(struct cmd_channels_status_op_result, + status,"enabled#disabled" ); + +cmdline_parse_inst_t cmd_channels_status_op_set = { + .f = cmd_channels_status_op_parsed, + .data =NULL , + .help_str ="set_channel_status <vm_name> <list>|all enabled|disabled, " +" enable or disable the communication channels in " +"list(comma-separated) for the specified VM, alternatively " +"list can be replaced with keyword 'all'. " +"Disabled channels will still receive packets on the host, " +"however the commands they specify will be ignored. " +"Set status to 'enabled' to begin processing requests again." , + .tokens = { + (void *)&cmd_channels_status_op, + (void *)&cmd_channels_status_vm_name, + (void *)&cmd_channels_status_list, + (void *)&cmd_channels_status, +NULL , + }, +}; + +/* *** CPU Frequency operations *** */ +struct cmd_show_cpu_freq_mask_result { +cmdline_fixed_string_t show_cpu_freq_mask; +uint64_t core_mask; +}; + +static void +cmd_show_cpu_freq_mask_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_show_cpu_freq_mask_result *res = parsed_result; +unsigned i; +uint64_t mask = res->core_mask; +uint32_t freq; + +for (i =0 ; mask; mask &= ~(1ULL << i++)) { +if ((mask >> i) &1 ) { + freq =power_manager_get_current_frequency (i); +if (freq >0 ) +cmdline_printf (cl,"Core %u: %" PRId32"\n" , i, freq); + } + } +} + +cmdline_parse_token_string_t cmd_show_cpu_freq_mask = + TOKEN_STRING_INITIALIZER(struct cmd_show_cpu_freq_mask_result, + show_cpu_freq_mask,"show_cpu_freq_mask" ); +cmdline_parse_token_num_t cmd_show_cpu_freq_mask_core_mask = + TOKEN_NUM_INITIALIZER(struct cmd_show_cpu_freq_mask_result, + core_mask,UINT64 ); + +cmdline_parse_inst_t cmd_show_cpu_freq_mask_set = { + .f = cmd_show_cpu_freq_mask_parsed, + .data =NULL , + .help_str ="show_cpu_freq_mask <mask>, Get the current frequency for each " +"core specified in the mask" , + .tokens = { + (void *)&cmd_show_cpu_freq_mask, + (void *)&cmd_show_cpu_freq_mask_core_mask, +NULL , + }, +}; + +struct cmd_set_cpu_freq_mask_result { +cmdline_fixed_string_t set_cpu_freq_mask; +uint64_t core_mask; +cmdline_fixed_string_t cmd; +}; + +static void +cmd_set_cpu_freq_mask_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_set_cpu_freq_mask_result *res = parsed_result; +int ret =-1 ; + +if (!strcmp (res->cmd ,"up" )) + ret =power_manager_scale_mask_up (res->core_mask); +else if (!strcmp (res->cmd ,"down" )) + ret =power_manager_scale_mask_down (res->core_mask); +else if (!strcmp (res->cmd ,"min" )) + ret =power_manager_scale_mask_min (res->core_mask); +else if (!strcmp (res->cmd ,"max" )) + ret =power_manager_scale_mask_max (res->core_mask); +if (ret <0 ) { +cmdline_printf (cl,"Error scaling core_mask(0x%" PRIx64") '%s' , not " +"all cores specified have been scaled\n" , + res->core_mask, res->cmd); + }; +} + +cmdline_parse_token_string_t cmd_set_cpu_freq_mask = + TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_mask_result, + set_cpu_freq_mask,"set_cpu_freq_mask" ); +cmdline_parse_token_num_t cmd_set_cpu_freq_mask_core_mask = + TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_mask_result, + core_mask,UINT64 ); +cmdline_parse_token_string_t cmd_set_cpu_freq_mask_result = + TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_mask_result, + cmd,"up#down#min#max" ); + +cmdline_parse_inst_t cmd_set_cpu_freq_mask_set = { + .f = cmd_set_cpu_freq_mask_parsed, + .data =NULL , + .help_str ="set_cpu_freq <core_mask> <up|down|min|max>, Set the current " +"frequency for the cores specified in <core_mask> by scaling " +"each up/down/min/max." , + .tokens = { + (void *)&cmd_set_cpu_freq_mask, + (void *)&cmd_set_cpu_freq_mask_core_mask, + (void *)&cmd_set_cpu_freq_mask_result, +NULL , + }, +}; + + + +struct cmd_show_cpu_freq_result { +cmdline_fixed_string_t show_cpu_freq; +uint8_t core_num; +}; + +static void +cmd_show_cpu_freq_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +struct cmd_show_cpu_freq_result *res = parsed_result; +uint32_t curr_freq =power_manager_get_current_frequency (res->core_num); + +if (curr_freq ==0 ) { +cmdline_printf (cl,"Unable to get frequency for core %u\n" , + res->core_num); +return ; + } +cmdline_printf (cl,"Core %u frequency: %" PRId32"\n" , res->core_num, + curr_freq); +} + +cmdline_parse_token_string_t cmd_show_cpu_freq = + TOKEN_STRING_INITIALIZER(struct cmd_show_cpu_freq_result, + show_cpu_freq,"show_cpu_freq" ); + +cmdline_parse_token_num_t cmd_show_cpu_freq_core_num = + TOKEN_NUM_INITIALIZER(struct cmd_show_cpu_freq_result, + core_num,UINT8 ); + +cmdline_parse_inst_t cmd_show_cpu_freq_set = { + .f = cmd_show_cpu_freq_parsed, + .data =NULL , + .help_str ="Get the current frequency for the specified core" , + .tokens = { + (void *)&cmd_show_cpu_freq, + (void *)&cmd_show_cpu_freq_core_num, +NULL , + }, +}; + +struct cmd_set_cpu_freq_result { +cmdline_fixed_string_t set_cpu_freq; +uint8_t core_num; +cmdline_fixed_string_t cmd; +}; + +static void +cmd_set_cpu_freq_parsed (void *parsed_result,struct cmdline *cl, + __attribute__((unused))void *data) +{ +int ret =-1 ; +struct cmd_set_cpu_freq_result *res = parsed_result; + +if (!strcmp (res->cmd ,"up" )) + ret =power_manager_scale_core_up (res->core_num); +else if (!strcmp (res->cmd ,"down" )) + ret =power_manager_scale_core_down (res->core_num); +else if (!strcmp (res->cmd ,"min" )) + ret =power_manager_scale_core_min (res->core_num); +else if (!strcmp (res->cmd ,"max" )) + ret =power_manager_scale_core_max (res->core_num); +if (ret <0 ) { +cmdline_printf (cl,"Error scaling core(%u) '%s'\n" , res->core_num, + res->cmd); + } +} + +cmdline_parse_token_string_t cmd_set_cpu_freq = + TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result, + set_cpu_freq,"set_cpu_freq" ); +cmdline_parse_token_string_t cmd_set_cpu_freq_core_num = + TOKEN_NUM_INITIALIZER(struct cmd_set_cpu_freq_result, + core_num,UINT8 ); +cmdline_parse_token_string_t cmd_set_cpu_freq_cmd_cmd = + TOKEN_STRING_INITIALIZER(struct cmd_set_cpu_freq_result, + cmd,"up#down#min#max" ); + +cmdline_parse_inst_t cmd_set_cpu_freq_set = { + .f = cmd_set_cpu_freq_parsed, + .data =NULL , + .help_str ="set_cpu_freq <core_num> <up|down|min|max>, Set the current " +"frequency for the specified core by scaling up/down/min/max" , + .tokens = { + (void *)&cmd_set_cpu_freq, + (void *)&cmd_set_cpu_freq_core_num, + (void *)&cmd_set_cpu_freq_cmd_cmd, +NULL , + }, +}; + +cmdline_parse_ctx_t main_ctx[] = { + (cmdline_parse_inst_t *)&cmd_quit, + (cmdline_parse_inst_t *)&cmd_vm_op_set, + (cmdline_parse_inst_t *)&cmd_channels_op_set, + (cmdline_parse_inst_t *)&cmd_channels_status_op_set, + (cmdline_parse_inst_t *)&cmd_show_vm_set, + (cmdline_parse_inst_t *)&cmd_show_cpu_freq_mask_set, + (cmdline_parse_inst_t *)&cmd_set_cpu_freq_mask_set, + (cmdline_parse_inst_t *)&cmd_show_cpu_freq_set, + (cmdline_parse_inst_t *)&cmd_set_cpu_freq_set, + (cmdline_parse_inst_t *)&cmd_set_pcpu_mask_set, + (cmdline_parse_inst_t *)&cmd_set_pcpu_set, +NULL , +}; + +void +run_cli (__attribute__((unused))void *arg) +{ +struct cmdline *cl; + + cl =cmdline_stdin_new (main_ctx,"vmpower> " ); +if (cl ==NULL ) +return ; + +cmdline_interact (cl); +cmdline_stdin_exit (cl); +}
examples/vm_power_manager/vm_power_cli.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v6 3/10 CPU Frequency Power Management(Host).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
A wrapper around librte_power(using ACPI cpufreq), providing locking around the non-threadsafe library, allowing for frequency changes based on core masks and core numbers from both the CLI thread and epoll monitor thread.
--- examples/vm_power_manager/power_manager.c | 253 +++++++++++++++++++++++++++++ examples/vm_power_manager/power_manager.h | 188 +++++++++++++++++++++ 2 files changed, 441 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/power_manager.c create mode 100644 examples/vm_power_manager/power_manager.h
examples/vm_power_manager/power_manager.c new file +253 -0
@@ -0,0 +1,253 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <inttypes.h> +#include <sys/un.h> +#include <fcntl.h> +#include <unistd.h> +#include <dirent.h> +#include <errno.h> + +#include <sys/types.h> + +#include <rte_config.h> +#include <rte_log.h> +#include <rte_power.h> +#include <rte_spinlock.h> + +#include "power_manager.h" + +#define RTE_LOGTYPE_POWER_MANAGER RTE_LOGTYPE_USER1 + +#define POWER_SCALE_CORE (DIRECTION , core_num , ret) do { \ + if (core_num >= POWER_MGR_MAX_CPUS) \ + return -1; \ + if (!(global_enabled_cpus & (1ULL << core_num))) \ + return -1; \ + rte_spinlock_lock(&global_core_freq_info[core_num].power_sl); \ + ret = rte_power_freq_##DIRECTION(core_num); \ + rte_spinlock_unlock(&global_core_freq_info[core_num].power_sl); \ +} while (0) + +#define POWER_SCALE_MASK (DIRECTION , core_mask, ret) do { \ + int i; \ + for (i = 0; core_mask; core_mask &= ~(1 << i++)) { \ + if ((core_mask >> i) & 1) { \ + if (!(global_enabled_cpus & (1ULL << i))) \ + continue; \ + rte_spinlock_lock(&global_core_freq_info[i].power_sl); \ + if (rte_power_freq_##DIRECTION(i) != 1) \ + ret = -1; \ + rte_spinlock_unlock(&global_core_freq_info[i].power_sl); \ + } \ + } \ +} while (0) + +struct freq_info { +rte_spinlock_t power_sl; +uint32_t freqs[RTE_MAX_LCORE_FREQS ]; +unsigned num_freqs; +} __rte_cache_aligned; + +static struct freq_info global_core_freq_info[POWER_MGR_MAX_CPUS ]; + +static uint64_t global_enabled_cpus; + +#define SYSFS_CPU_PATH "/sys/devices/system/cpu/cpu%u/topology/core_id" + +static unsigned +set_host_cpus_mask (void ) +{ +char path[PATH_MAX ]; +unsigned i; +unsigned num_cpus =0 ; + +for (i =0 ; i <POWER_MGR_MAX_CPUS ; i++) { +snprintf (path,sizeof (path),SYSFS_CPU_PATH , i); +if (access (path,F_OK ) ==0 ) { + global_enabled_cpus |=1ULL << i; + num_cpus++; + }else +return num_cpus; + } +return num_cpus; +} + +int +power_manager_init (void ) +{ +unsigned i, num_cpus; +uint64_t cpu_mask; +int ret =0 ; + + num_cpus =set_host_cpus_mask (); +if (num_cpus ==0 ) { +RTE_LOG (ERR ,POWER_MANAGER ,"Unable to detected host CPUs, please " +"ensure that sufficient privileges exist to inspect sysfs\n" ); +return -1 ; + } +rte_power_set_env (PM_ENV_ACPI_CPUFREQ ); + cpu_mask = global_enabled_cpus; +for (i =0 ; cpu_mask; cpu_mask &= ~(1 << i++)) { +if (rte_power_init (i) <0 ||rte_power_freqs (i, + global_core_freq_info[i].freqs, +RTE_MAX_LCORE_FREQS ) ==0 ) { +RTE_LOG (ERR ,POWER_MANAGER ,"Unable to initialize power manager " +"for core %u\n" , i); + global_enabled_cpus &= ~(1 << i); + num_cpus--; + ret =-1 ; + } +rte_spinlock_init (&global_core_freq_info[i].power_sl); + } +RTE_LOG (INFO ,POWER_MANAGER ,"Detected %u host CPUs , enabled core mask:" +" 0x%" PRIx64"\n" , num_cpus, global_enabled_cpus); +return ret; + +} + +uint32_t +power_manager_get_current_frequency (unsigned core_num) +{ +uint32_t freq, index; + +if (core_num >=POWER_MGR_MAX_CPUS ) { +RTE_LOG (ERR ,POWER_MANAGER ,"Core(%u) is out of range 0...%d\n" , + core_num,POWER_MGR_MAX_CPUS -1 ); +return -1 ; + } +if (!(global_enabled_cpus & (1ULL << core_num))) +return 0 ; + +rte_spinlock_lock (&global_core_freq_info[core_num].power_sl); + index =rte_power_get_freq (core_num); +rte_spinlock_unlock (&global_core_freq_info[core_num].power_sl); +if (index >=POWER_MGR_MAX_CPUS ) + freq =0 ; +else + freq = global_core_freq_info[core_num].freqs[index]; + +return freq; +} + +int +power_manager_exit (void ) +{ +unsigned int i; +int ret =0 ; + +for (i =0 ; global_enabled_cpus; global_enabled_cpus &= ~(1 << i++)) { +if (rte_power_exit (i) <0 ) { +RTE_LOG (ERR ,POWER_MANAGER ,"Unable to shutdown power manager " +"for core %u\n" , i); + ret =-1 ; + } + } + global_enabled_cpus =0 ; +return ret; +} + +int +power_manager_scale_mask_up (uint64_t core_mask) +{ +int ret =0 ; + +POWER_SCALE_MASK (up, core_mask, ret); +return ret; +} + +int +power_manager_scale_mask_down (uint64_t core_mask) +{ +int ret =0 ; + +POWER_SCALE_MASK (down, core_mask, ret); +return ret; +} + +int +power_manager_scale_mask_min (uint64_t core_mask) +{ +int ret =0 ; + +POWER_SCALE_MASK (min, core_mask, ret); +return ret; +} + +int +power_manager_scale_mask_max (uint64_t core_mask) +{ +int ret =0 ; + +POWER_SCALE_MASK (max, core_mask, ret); +return ret; +} + +int +power_manager_scale_core_up (unsigned core_num) +{ +int ret =0 ; + +POWER_SCALE_CORE (up, core_num, ret); +return ret; +} + +int +power_manager_scale_core_down (unsigned core_num) +{ +int ret =0 ; + +POWER_SCALE_CORE (down, core_num, ret); +return ret; +} + +int +power_manager_scale_core_min (unsigned core_num) +{ +int ret =0 ; + +POWER_SCALE_CORE (min, core_num, ret); +return ret; +} + +int +power_manager_scale_core_max (unsigned core_num) +{ +int ret =0 ; + +POWER_SCALE_CORE (max, core_num, ret); +return ret; +}
examples/vm_power_manager/power_manager.h new file +188 -0
188 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v6 4/10 VM Power Management application and Makefile.
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
For launching CLI thread and Monitor thread and initialising resources.
Requires a minimum of two lcores to run, additional cores specified by eal core mask are not used.
--- examples/vm_power_manager/Makefile | 57 +++++++++++++++++ examples/vm_power_manager/main.c | 117 ++++++++++++++++++++++++++++++++++++ examples/vm_power_manager/main.h | 52 ++++++++++++++++ 3 files changed, 226 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/Makefile create mode 100644 examples/vm_power_manager/main.c create mode 100644 examples/vm_power_manager/main.h
examples/vm_power_manager/Makefile new file +57 -0
@@ -0,0 +1,57 @@ +# BSD LICENSE +# +# Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +ifeq ($(RTE_SDK),) +$(error "Please define RTE_SDK environment variable") +endif + +# Default target, can be overridden by command line or environment +RTE_TARGET ?= x86_64-native-linuxapp-gcc + +include $(RTE_SDK)/mk/rte.vars.mk + +# binary name +APP = vm_power_mgr + +# all source are stored in SRCS-y +SRCS-y := main.c vm_power_cli.c power_manager.c channel_manager.c +SRCS-y += channel_monitor.c + +CFLAGS += -O3 -lvirt -I$(RTE_SDK)/lib/librte_power/ +CFLAGS += $(WERROR_FLAGS) + +# workaround for a gcc bug with noreturn attribute +# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12603 +ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y) +CFLAGS_main.o += -Wno-return-type +endif + +include $(RTE_SDK)/mk/rte.extapp.mk
examples/vm_power_manager/main.c new file +117 -0
117 lines not on this page · raw message
examples/vm_power_manager/main.h new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v6 5/10 VM Power Management CLI(Guest).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Provides a small sample application(guest_vm_power_mgr) to run on a VM. The application is run by providing a core mask(-c) and number of memory channels(-n). The core mask corresponds to the number of lcore channels to attempt to open. A maximum of 64 channels per VM is allowed. The channels must be monitored by the host.
After successful initialisation a CPU frequency command can be sent to the host using:set_cpu_freq <lcore_num> <up|down|min|max>.
--- examples/vm_power_manager/guest_cli/Makefile | 56 +++++++ examples/vm_power_manager/guest_cli/main.c | 88 +++++++++++ examples/vm_power_manager/guest_cli/main.h | 52 +++++++ .../guest_cli/vm_power_cli_guest.c | 156 ++++++++++++++++++++ .../guest_cli/vm_power_cli_guest.h | 55 +++++++ 5 files changed, 407 insertions(+), 0 deletions(-) create mode 100644 examples/vm_power_manager/guest_cli/Makefile create mode 100644 examples/vm_power_manager/guest_cli/main.c create mode 100644 examples/vm_power_manager/guest_cli/main.h create mode 100644 examples/vm_power_manager/guest_cli/vm_power_cli_guest.c create mode 100644 examples/vm_power_manager/guest_cli/vm_power_cli_guest.h
examples/vm_power_manager/guest_cli/Makefile new file +56 -0
@@ -0,0 +1,56 @@ +# BSD LICENSE +# +# Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +ifeq ($(RTE_SDK),) +$(error "Please define RTE_SDK environment variable") +endif + +# Default target, can be overridden by command line or environment +RTE_TARGET ?= x86_64-native-linuxapp-gcc + +include $(RTE_SDK)/mk/rte.vars.mk + +# binary name +APP = guest_vm_power_mgr + +# all source are stored in SRCS-y +SRCS-y := main.c vm_power_cli_guest.c + +CFLAGS += -O3 -I$(RTE_SDK)/lib/librte_power/ +CFLAGS += $(WERROR_FLAGS) + +# workaround for a gcc bug with noreturn attribute +# http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12603 +ifeq ($(CONFIG_RTE_TOOLCHAIN_GCC),y) +CFLAGS_main.o += -Wno-return-type +endif + +include $(RTE_SDK)/mk/rte.extapp.mk
examples/vm_power_manager/guest_cli/main.c new file +88 -0
88 lines not on this page · raw message
examples/vm_power_manager/guest_cli/main.h new file +52 -0
52 lines not on this page · raw message
examples/vm_power_manager/guest_cli/vm_power_cli_guest.c new file +156 -0
156 lines not on this page · raw message
examples/vm_power_manager/guest_cli/vm_power_cli_guest.h new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v6 6/10 VM communication channels for VM Power Management(Guest).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Allows for the opening of Virtio-Serial devices on a VM, where a DPDK application can send packets to the host based monitor. The packet formatted is specified in channel_commands.h
Each device appears as a serial device in path /dev/virtio-ports/virtio.serial.port.<agent_type>.<lcore_num> where each lcore in a DPDK application has exclusive to a device/channel.
Each channel is opened in non-blocking mode, after a successful open a test packet is send to the host to ensure the host side is monitoring.
--- lib/librte_power/guest_channel.c | 162 ++++++++++++++++++++++++++++++++++++++ lib/librte_power/guest_channel.h | 89 +++++++++++++++++++++ 2 files changed, 251 insertions(+), 0 deletions(-) create mode 100644 lib/librte_power/guest_channel.c create mode 100644 lib/librte_power/guest_channel.h
lib/librte_power/guest_channel.c new file +162 -0
@@ -0,0 +1,162 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <signal.h> +#include <limits.h> +#include <fcntl.h> +#include <string.h> +#include <errno.h> + + +#include <rte_log.h> +#include <rte_config.h> + +#include "guest_channel.h" +#include "channel_commands.h" + +#define RTE_LOGTYPE_GUEST_CHANNEL RTE_LOGTYPE_USER1 + +static int global_fds[RTE_MAX_LCORE ]; + +int +guest_channel_host_connect (const char *path,unsigned lcore_id) +{ +int flags, ret; +struct channel_packet pkt; +char fd_path[PATH_MAX ]; +int fd =-1 ; + +if (lcore_id >=RTE_MAX_LCORE ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel(%u) is out of range 0...%d\n" , + lcore_id,RTE_MAX_LCORE -1 ); +return -1 ; + } +/* check if path is already open */ +if (global_fds[lcore_id] !=0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel(%u) is already open with fd %d\n" , + lcore_id, global_fds[lcore_id]); +return -1 ; + } + +snprintf (fd_path,PATH_MAX ,"%s.%u" , path, lcore_id); +RTE_LOG (INFO ,GUEST_CHANNEL ,"Opening channel '%s' for lcore %u\n" , + fd_path, lcore_id); + fd =open (fd_path,O_RDWR ); +if (fd <0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Unable to to connect to '%s' with error " +"%s\n" , fd_path,strerror (errno)); +return -1 ; + } + + flags =fcntl (fd,F_GETFL ,0 ); +if (flags <0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Failed on fcntl get flags for file %s\n" , + fd_path); + goto error; + } + + flags |=O_NONBLOCK ; +if (fcntl (fd,F_SETFL , flags) <0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Failed on setting non-blocking mode for " +"file %s" , fd_path); + goto error; + } +/* QEMU needs a delay after connection */ +sleep (1 ); + +/* Send a test packet, this command is ignored by the host, but a successful +* send indicates that the host endpoint is monitoring. +*/ + pkt.command =CPU_POWER_CONNECT ; + global_fds[lcore_id] = fd; + ret =guest_channel_send_msg (&pkt, lcore_id); +if (ret !=0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Error on channel '%s' communications " +"test: %s\n" , fd_path,strerror (ret)); + goto error; + } +RTE_LOG (INFO ,GUEST_CHANNEL ,"Channel '%s' is now connected\n" , fd_path); +return 0 ; +error: +close (fd); + global_fds[lcore_id] =0 ; +return -1 ; +} + +int +guest_channel_send_msg (struct channel_packet *pkt,unsigned lcore_id) +{ +int ret, buffer_len =sizeof (*pkt); +void *buffer = pkt; + +if (lcore_id >=RTE_MAX_LCORE ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel(%u) is out of range 0...%d\n" , + lcore_id,RTE_MAX_LCORE -1 ); +return -1 ; + } + +if (global_fds[lcore_id] ==0 ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel is not connected\n" ); +return -1 ; + } +while (buffer_len >0 ) { + ret =write (global_fds[lcore_id], buffer, buffer_len); +if (ret == buffer_len) +return 0 ; +if (ret ==-1 ) { +if (errno ==EINTR ) +continue ; +return errno; + } + buffer = (char *)buffer + ret; + buffer_len -= ret; + } +return 0 ; +} + +void +guest_channel_host_disconnect (unsigned lcore_id) +{ +if (lcore_id >=RTE_MAX_LCORE ) { +RTE_LOG (ERR ,GUEST_CHANNEL ,"Channel(%u) is out of range 0...%d\n" , + lcore_id,RTE_MAX_LCORE -1 ); +return ; + } +if (global_fds[lcore_id] ==0 ) +return ; +close (global_fds[lcore_id]); + global_fds[lcore_id] =0 ; +}
lib/librte_power/guest_channel.h new file +89 -0
89 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v6 7/10 librte_power common interface for Guest and Host
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Moved the current librte_power implementation to rte_power_acpi_cpufreq, with renaming of functions only.
Added rte_power_kvm_vm implementation to support Power Management from a VM.
librte_power now hides the implementation based on the environment used. A new call rte_power_set_env() can explicidly set the environment, if not called then auto-detection takes place.
rte_power_kvm_vm is subset of the librte_power APIs, the following is supported:
rte_power_init(unsigned lcore_id)
rte_power_exit(unsigned lcore_id)
rte_power_freq_up(unsigned lcore_id)
rte_power_freq_down(unsigned lcore_id)
rte_power_freq_min(unsigned lcore_id)
rte_power_freq_max(unsigned lcore_id)
The other unsupported APIs return -ENOTSUP
--- lib/librte_power/rte_power.c | 540 ++++------------------------- lib/librte_power/rte_power.h | 120 +++++-- lib/librte_power/rte_power_acpi_cpufreq.c | 545 +++++++++++++++++++++++++++++ lib/librte_power/rte_power_acpi_cpufreq.h | 192 ++++++++++ lib/librte_power/rte_power_common.h | 39 ++ lib/librte_power/rte_power_kvm_vm.c | 136 +++++++ lib/librte_power/rte_power_kvm_vm.h | 179 ++++++++++ 7 files changed, 1249 insertions(+), 502 deletions(-) create mode 100644 lib/librte_power/rte_power_acpi_cpufreq.c create mode 100644 lib/librte_power/rte_power_acpi_cpufreq.h create mode 100644 lib/librte_power/rte_power_common.h create mode 100644 lib/librte_power/rte_power_kvm_vm.c create mode 100644 lib/librte_power/rte_power_kvm_vm.h
lib/librte_power/rte_power.c +69 -471
@@ -31,515 +31,113 @@* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <stdio.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> -#include <signal.h> -#include <limits.h> - -#include <rte_memcpy.h> #include <rte_atomic.h> #include "rte_power.h" +#include "rte_power_acpi_cpufreq.h" +#include "rte_power_kvm_vm.h" +#include "rte_power_common.h" -#ifdef RTE_LIBRTE_POWER_DEBUG -#define POWER_DEBUG_TRACE(fmt, args...) do { \ - RTE_LOG(ERR, POWER, "%s: " fmt, __func__, ## args); \ - } while (0) -#else -#define POWER_DEBUG_TRACE(fmt, args...) -#endif - -#define FOPEN_OR_ERR_RET (f, retval) do { \ - if ((f) == NULL) { \ - RTE_LOG(ERR, POWER, "File not openned\n"); \ - return (retval); \ - } \ -} while(0) - -#define FOPS_OR_NULL_GOTO (ret, label) do { \ - if ((ret) == NULL) { \ - RTE_LOG(ERR, POWER, "fgets returns nothing\n"); \ - goto label; \ - } \ -} while(0) - -#define FOPS_OR_ERR_GOTO (ret, label) do { \ - if ((ret) < 0) { \ - RTE_LOG(ERR, POWER, "File operations failed\n"); \ - goto label; \ - } \ -} while(0) - -#define STR_SIZE 1024 -#define POWER_CONVERT_TO_DECIMAL 10 +enum power_management_env global_default_env =PM_ENV_NOT_SET ; -#define POWER_GOVERNOR_USERSPACE "userspace" -#define POWER_SYSFILE_GOVERNOR \ - "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_governor" -#define POWER_SYSFILE_AVAIL_FREQ \ - "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies" -#define POWER_SYSFILE_SETSPEED \ - "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed" +volatile uint32_t global_env_cfg_status =0 ; -enum power_state { -POWER_IDLE =0 , -POWER_ONGOING , -POWER_USED , -POWER_UNKNOWN -}; +/* function pointers */ +rte_power_freqs_t rte_power_freqs =NULL ; +rte_power_get_freq_t rte_power_get_freq =NULL ; +rte_power_set_freq_t rte_power_set_freq =NULL ; +rte_power_freq_change_t rte_power_freq_up =NULL ; +rte_power_freq_change_t rte_power_freq_down =NULL ; +rte_power_freq_change_t rte_power_freq_max =NULL ; +rte_power_freq_change_t rte_power_freq_min =NULL ; -/** -* Power info per lcore. -*/ -struct rte_power_info { -unsigned lcore_id;/**< Logical core id */ -uint32_t freqs[RTE_MAX_LCORE_FREQS ];/**< Frequency array */ -uint32_t nb_freqs;/**< number of available freqs */ -FILE *f;/**< FD of scaling_setspeed */ -char governor_ori[32 ];/**< Original governor name */ -uint32_t curr_idx;/**< Freq index in freqs array */ -volatile uint32_t state;/**< Power in use state */ -} __rte_cache_aligned; - -static struct rte_power_info lcore_power_info[RTE_MAX_LCORE ]; - -/** -* It is to set specific freq for specific logical core, according to the index -* of supported frequencies. -*/ -static int -set_freq_internal (struct rte_power_info *pi,uint32_t idx) +int +rte_power_set_env (enum power_management_env env) { -if (idx >=RTE_MAX_LCORE_FREQS || idx >= pi->nb_freqs) { -RTE_LOG (ERR ,POWER ,"Invalid frequency index %u, which " -"should be less than %u\n" , idx, pi->nb_freqs); -return -1 ; - } - -/* Check if it is the same as current */ -if (idx == pi->curr_idx) +if (rte_atomic32_cmpset (&global_env_cfg_status,0 ,1 ) ==0 ) {return 0 ; - -POWER_DEBUG_TRACE ("Freqency[%u] %u to be set for lcore %u\n" , - idx, pi->freqs[idx], pi->lcore_id); -if (fseek (pi->f,0 ,SEEK_SET ) <0 ) { -RTE_LOG (ERR ,POWER ,"Fail to set file position indicator to 0 " -"for setting frequency for lcore %u\n" , pi->lcore_id); -return -1 ; } -if (fprintf (pi->f,"%u" , pi->freqs[idx]) <0 ) { -RTE_LOG (ERR ,POWER ,"Fail to write new frequency for " -"lcore %u\n" , pi->lcore_id); +if (env ==PM_ENV_ACPI_CPUFREQ ) { + rte_power_freqs = rte_power_acpi_cpufreq_freqs; + rte_power_get_freq = rte_power_acpi_cpufreq_get_freq; + rte_power_set_freq = rte_power_acpi_cpufreq_set_freq; + rte_power_freq_up = rte_power_acpi_cpufreq_freq_up; + rte_power_freq_down = rte_power_acpi_cpufreq_freq_down; + rte_power_freq_min = rte_power_acpi_cpufreq_freq_min; + rte_power_freq_max = rte_power_acpi_cpufreq_freq_max; + }else if (env ==PM_ENV_KVM_VM ) { + rte_power_freqs = rte_power_kvm_vm_freqs; + rte_power_get_freq = rte_power_kvm_vm_get_freq; + rte_power_set_freq = rte_power_kvm_vm_set_freq; + rte_power_freq_up = rte_power_kvm_vm_freq_up; + rte_power_freq_down = rte_power_kvm_vm_freq_down; + rte_power_freq_min = rte_power_kvm_vm_freq_min; + rte_power_freq_max = rte_power_kvm_vm_freq_max; + }else { +RTE_LOG (ERR ,POWER ,"Invalid Power Management Environment(%d) set\n" , + env); +rte_power_unset_env ();return -1 ; } -fflush (pi->f); - pi->curr_idx = idx; - -return 1 ; -} - -/** -* It is to check the current scaling governor by reading sys file, and then -* set it into 'userspace' if it is not by writing the sys file. The original -* governor will be saved for rolling back. -*/ -static int -power_set_governor_userspace (struct rte_power_info *pi) -{ -FILE *f; -int ret =-1 ; -char buf[BUFSIZ ]; -char fullpath[PATH_MAX ]; -char *s; -int val; - -snprintf (fullpath,sizeof (fullpath),POWER_SYSFILE_GOVERNOR , - pi->lcore_id); - f =fopen (fullpath,"rw+" ); -FOPEN_OR_ERR_RET (f, ret); - - s =fgets (buf,sizeof (buf), f); -FOPS_OR_NULL_GOTO (s, out); - -/* Check if current governor is userspace */ -if (strncmp (buf,POWER_GOVERNOR_USERSPACE , -sizeof (POWER_GOVERNOR_USERSPACE )) ==0 ) { - ret =0 ; -POWER_DEBUG_TRACE ("Power management governor of lcore %u is " -"already userspace\n" , pi->lcore_id); - goto out; - } -/* Save the original governor */ -snprintf (pi->governor_ori,sizeof (pi->governor_ori),"%s" , buf); - -/* Write 'userspace' to the governor */ - val =fseek (f,0 ,SEEK_SET ); -FOPS_OR_ERR_GOTO (val, out); - - val =fputs (POWER_GOVERNOR_USERSPACE , f); -FOPS_OR_ERR_GOTO (val, out); - - ret =0 ; -RTE_LOG (INFO ,POWER ,"Power management governor of lcore %u has been " -"set to user space successfully\n" , pi->lcore_id); -out: -fclose (f); + global_default_env = env; +return 0 ; -return ret; } -/** -* It is to get the available frequencies of the specific lcore by reading the -* sys file. -*/ -static int -power_get_available_freqs (struct rte_power_info *pi) +void +rte_power_unset_env (void ) { -FILE *f; -int ret =-1 , i, count; -char *p; -char buf[BUFSIZ ]; -char fullpath[PATH_MAX ]; -char *freqs[RTE_MAX_LCORE_FREQS ]; -char *s; - -snprintf (fullpath,sizeof (fullpath),POWER_SYSFILE_AVAIL_FREQ , - pi->lcore_id); - f =fopen (fullpath,"r" ); -FOPEN_OR_ERR_RET (f, ret); - - s =fgets (buf,sizeof (buf), f); -FOPS_OR_NULL_GOTO (s, out); - -/* Strip the line break if there is */ - p =strchr (buf,'\n' ); -if (p !=NULL ) - *p =0 ; - -/* Split string into at most RTE_MAX_LCORE_FREQS frequencies */ - count =rte_strsplit (buf,sizeof (buf), freqs, -RTE_MAX_LCORE_FREQS ,' ' ); -if (count <=0 ) { -RTE_LOG (ERR ,POWER ,"No available frequency in " -"" POWER_SYSFILE_AVAIL_FREQ "\n" , pi->lcore_id); - goto out; - } -if (count >=RTE_MAX_LCORE_FREQS ) { -RTE_LOG (ERR ,POWER ,"Too many available frequencies : %d\n" , - count); - goto out; - } - -/* Store the available frequncies into power context */ -for (i =0 , pi->nb_freqs =0 ; i < count; i++) { -POWER_DEBUG_TRACE ("Lcore %u frequency[%d]: %s\n" , pi->lcore_id, - i, freqs[i]); - pi->freqs[pi->nb_freqs++] =strtoul (freqs[i], &p, -POWER_CONVERT_TO_DECIMAL ); - } - - ret =0 ; -POWER_DEBUG_TRACE ("%d frequencie(s) of lcore %u are available\n" , - count, pi->lcore_id); -out: -fclose (f); - -return ret; +if (rte_atomic32_cmpset (&global_env_cfg_status,1 ,0 ) !=0 ) + global_default_env =PM_ENV_NOT_SET ; } -/** -* It is to fopen the sys file for the future setting the lcore frequency. -*/ -static int -power_init_for_setting_freq (struct rte_power_info *pi) -{ -FILE *f; -char fullpath[PATH_MAX ]; -char buf[BUFSIZ ]; -uint32_t i, freq; -char *s; - -snprintf (fullpath,sizeof (fullpath),POWER_SYSFILE_SETSPEED , - pi->lcore_id); - f =fopen (fullpath,"rw+" ); -FOPEN_OR_ERR_RET (f,-1 ); - - s =fgets (buf,sizeof (buf), f); -FOPS_OR_NULL_GOTO (s, out); - - freq =strtoul (buf,NULL ,POWER_CONVERT_TO_DECIMAL ); -for (i =0 ; i < pi->nb_freqs; i++) { -if (freq == pi->freqs[i]) { - pi->curr_idx = i; - pi->f = f; -return 0 ; - } - } - -out: -fclose (f); - -return -1 ; +enum power_management_env +rte_power_get_env (void ) { +return global_default_env; }int rte_power_init (unsigned lcore_id) { -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Lcore id %u can not exceeds %u\n" , - lcore_id,RTE_MAX_LCORE -1U ); -return -1 ; - } - - pi = &lcore_power_info[lcore_id]; -if (rte_atomic32_cmpset (&(pi->state),POWER_IDLE ,POWER_ONGOING ) - ==0 ) { -RTE_LOG (INFO ,POWER ,"Power management of lcore %u is " -"in use\n" , lcore_id); -return -1 ; - } - - pi->lcore_id = lcore_id; -/* Check and set the governor */ -if (power_set_governor_userspace (pi) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot set governor of lcore %u to " -"userspace\n" , lcore_id); - goto fail; - } +int ret =-1 ; -/* Get the available frequencies */ -if (power_get_available_freqs (pi) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot get available frequencies of " -"lcore %u\n" , lcore_id); - goto fail; +if (global_default_env ==PM_ENV_ACPI_CPUFREQ ) { +return rte_power_acpi_cpufreq_init (lcore_id); } - -/* Init for setting lcore frequency */ -if (power_init_for_setting_freq (pi) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot init for setting frequency for " -"lcore %u\n" , lcore_id); - goto fail; +if (global_default_env ==PM_ENV_KVM_VM ) { +return rte_power_kvm_vm_init (lcore_id); } - -/* Set freq to max by default */ -if (rte_power_freq_max (lcore_id) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot set frequency of lcore %u " -"to max\n" , lcore_id); - goto fail; +/* Auto detect Environment */ +RTE_LOG (INFO ,POWER ,"Attempting to initialise ACPI cpufreq power " +"management...\n" ); + ret =rte_power_acpi_cpufreq_init (lcore_id); +if (ret ==0 ) { +rte_power_set_env (PM_ENV_ACPI_CPUFREQ ); + goto out; } -RTE_LOG (INFO ,POWER ,"Initialized successfully for lcore %u " -"power manamgement\n" , lcore_id); -rte_atomic32_cmpset (&(pi->state),POWER_ONGOING ,POWER_USED ); - -return 0 ; - -fail: -rte_atomic32_cmpset (&(pi->state),POWER_ONGOING ,POWER_UNKNOWN ); - -return -1 ; -} - -/** -* It is to check the governor and then set the original governor back if -* needed by writing the the sys file. -*/ -static int -power_set_governor_original (struct rte_power_info *pi) -{ -FILE *f; -int ret =-1 ; -char buf[BUFSIZ ]; -char fullpath[PATH_MAX ]; -char *s; -int val; - -snprintf (fullpath,sizeof (fullpath),POWER_SYSFILE_GOVERNOR , - pi->lcore_id); - f =fopen (fullpath,"rw+" ); -FOPEN_OR_ERR_RET (f, ret); - - s =fgets (buf,sizeof (buf), f); -FOPS_OR_NULL_GOTO (s, out); - -/* Check if the governor to be set is the same as current */ -if (strncmp (buf, pi->governor_ori,sizeof (pi->governor_ori)) ==0 ) { - ret =0 ; -POWER_DEBUG_TRACE ("Power management governor of lcore %u " -"has already been set to %s\n" , - pi->lcore_id, pi->governor_ori); +RTE_LOG (INFO ,POWER ,"Attempting to initialise VM power management...\n" ); + ret =rte_power_kvm_vm_init (lcore_id); +if (ret ==0 ) { +rte_power_set_env (PM_ENV_KVM_VM ); goto out; } - -/* Write back the original governor */ - val =fseek (f,0 ,SEEK_SET ); -FOPS_OR_ERR_GOTO (val, out); - - val =fputs (pi->governor_ori, f); -FOPS_OR_ERR_GOTO (val, out); - - ret =0 ; -RTE_LOG (INFO ,POWER ,"Power manamgement governor of lcore %u " -"has been set back to %s successfully\n" , - pi->lcore_id, pi->governor_ori); +RTE_LOG (ERR ,POWER ,"Unable to set Power Management Environment for lcore " +"%u\n" , lcore_id); out: -fclose (f); -return ret; }int rte_power_exit (unsigned lcore_id) { -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Lcore id %u can not exceeds %u\n" , - lcore_id,RTE_MAX_LCORE -1U ); -return -1 ; - } - pi = &lcore_power_info[lcore_id]; -if (rte_atomic32_cmpset (&(pi->state),POWER_USED ,POWER_ONGOING ) - ==0 ) { -RTE_LOG (INFO ,POWER ,"Power management of lcore %u is " -"not used\n" , lcore_id); -return -1 ; - } - -/* Close FD of setting freq */ -fclose (pi->f); - pi->f =NULL ; - -/* Set the governor back to the original */ -if (power_set_governor_original (pi) <0 ) { -RTE_LOG (ERR ,POWER ,"Cannot set the governor of %u back " -"to the original\n" , lcore_id); - goto fail; - } - -RTE_LOG (INFO ,POWER ,"Power management of lcore %u has exited from " -"'userspace' mode and been set back to the " -"original\n" , lcore_id); -rte_atomic32_cmpset (&(pi->state),POWER_ONGOING ,POWER_IDLE ); - -return 0 ; - -fail: -rte_atomic32_cmpset (&(pi->state),POWER_ONGOING ,POWER_UNKNOWN ); +if (global_default_env ==PM_ENV_ACPI_CPUFREQ ) +return rte_power_acpi_cpufreq_exit (lcore_id); +if (global_default_env ==PM_ENV_KVM_VM ) +return rte_power_kvm_vm_exit (lcore_id); +RTE_LOG (ERR ,POWER ,"Environment has not been set, unable to exit " +"gracefully\n" );return -1 ; -} - -uint32_t -rte_power_freqs (unsigned lcore_id,uint32_t *freqs,uint32_t num) -{ -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE || !freqs) { -RTE_LOG (ERR ,POWER ,"Invalid input parameter\n" ); -return 0 ; - } - - pi = &lcore_power_info[lcore_id]; -if (num < pi->nb_freqs) { -RTE_LOG (ERR ,POWER ,"Buffer size is not enough\n" ); -return 0 ; - } -rte_memcpy (freqs, pi->freqs, pi->nb_freqs *sizeof (uint32_t )); - -return pi->nb_freqs; -} - -uint32_t -rte_power_get_freq (unsigned lcore_id) -{ -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return RTE_POWER_INVALID_FREQ_INDEX ; - } - -return lcore_power_info[lcore_id].curr_idx; -} - -int -rte_power_set_freq (unsigned lcore_id,uint32_t index) -{ -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - -return set_freq_internal (&(lcore_power_info[lcore_id]), index); -} - -int -rte_power_freq_down (unsigned lcore_id) -{ -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - pi = &lcore_power_info[lcore_id]; -if (pi->curr_idx +1 == pi->nb_freqs) -return 0 ; - -/* Frequencies in the array are from high to low. */ -return set_freq_internal (pi, pi->curr_idx +1 ); } - -int -rte_power_freq_up (unsigned lcore_id) -{ -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - - pi = &lcore_power_info[lcore_id]; -if (pi->curr_idx ==0 ) -return 0 ; - -/* Frequencies in the array are from high to low. */ -return set_freq_internal (pi, pi->curr_idx -1 ); -} - -int -rte_power_freq_max (unsigned lcore_id) -{ -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - -/* Frequencies in the array are from high to low. */ -return set_freq_internal (&lcore_power_info[lcore_id],0 ); -} - -int -rte_power_freq_min (unsigned lcore_id) -{ -struct rte_power_info *pi; - -if (lcore_id >=RTE_MAX_LCORE ) { -RTE_LOG (ERR ,POWER ,"Invalid lcore ID\n" ); -return -1 ; - } - - pi = &lcore_power_info[lcore_id]; - -/* Frequencies in the array are from high to low. */ -return set_freq_internal (pi, pi->nb_freqs -1 ); -} -
lib/librte_power/rte_power.h +89 -31
214 lines not on this page · raw message
lib/librte_power/rte_power_acpi_cpufreq.c new file +545 -0
545 lines not on this page · raw message
lib/librte_power/rte_power_acpi_cpufreq.h new file +192 -0
192 lines not on this page · raw message
lib/librte_power/rte_power_common.h new file +39 -0
@@ -0,0 +1,39 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef RTE_POWER_COMMON_H_ +#define RTE_POWER_COMMON_H_ + +#define RTE_POWER_INVALID_FREQ_INDEX (~0) + +#endif /* RTE_POWER_COMMON_H_ */
lib/librte_power/rte_power_kvm_vm.c new file +136 -0
136 lines not on this page · raw message
lib/librte_power/rte_power_kvm_vm.h new file +179 -0
179 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v6 8/10 Packet format for VM Power Management(Host and Guest).
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Provides a command packet format for host and guest.
--- lib/librte_power/channel_commands.h | 77 +++++++++++++++++++++++++++++++++++ 1 files changed, 77 insertions(+), 0 deletions(-) create mode 100644 lib/librte_power/channel_commands.h
lib/librte_power/channel_commands.h new file +77 -0
@@ -0,0 +1,77 @@ +/*- +* BSD LICENSE +* +* Copyright(c) 2010-2014 Intel Corporation. All rights reserved. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* * Neither the name of Intel Corporation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef CHANNEL_COMMANDS_H_ +#define CHANNEL_COMMANDS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdint.h> + +/* Maximum number of CPUs */ +#define CHANNEL_CMDS_MAX_CPUS 64 +#if CHANNEL_CMDS_MAX_CPUS >64 +#error Maximum number of cores is 64, overflow is guaranteed to \ + cause problems with VM Power Management +#endif + +/* Maximum number of channels per VM */ +#define CHANNEL_CMDS_MAX_VM_CHANNELS 64 + +/* Maximum number of channels per VM */ +#define CHANNEL_CMDS_MAX_VM_CHANNELS 64 + +/* Valid Commands */ +#define CPU_POWER 1 +#define CPU_POWER_CONNECT 2 + +/* CPU Power Command Scaling */ +#define CPU_POWER_SCALE_UP 1 +#define CPU_POWER_SCALE_DOWN 2 +#define CPU_POWER_SCALE_MAX 3 +#define CPU_POWER_SCALE_MIN 4 + +struct channel_packet { +uint64_t resource_id;/**< core_num, device */ +uint32_t unit;/**< scale down/up/min/max */ +uint32_t command;/**< Power, IO, etc */ +}; + + +#ifdef __cplusplus +} +#endif + +#endif /* CHANNEL_COMMANDS_H_ */
Signature · 1 line
1.7.4.1
PATCH v6 9/10 Build system integration for VM Power Management(Guest and Host)
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
librte_power now contains both rte_power_acpi_cpufreq and rte_power_kvm_vm implementations.
--- lib/librte_power/Makefile | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
lib/librte_power/Makefile +2 -1
@@ -37,7 +37,8 @@ LIB = librte_power.a CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 -fno-strict-aliasing # all source are stored in SRCS-y -SRCS-$(CONFIG_RTE_LIBRTE_POWER) := rte_power.c +SRCS-$(CONFIG_RTE_LIBRTE_POWER) := rte_power.c rte_power_acpi_cpufreq.c +SRCS-$(CONFIG_RTE_LIBRTE_POWER) += rte_power_kvm_vm.c guest_channel.c # install this header file SYMLINK-$(CONFIG_RTE_LIBRTE_POWER)-include := rte_power.h
Signature · 1 line
1.7.4.1
PATCH v6 10/10 VM Power Management Unit Tests
In reply to a message not in the archiveFrom: Alan Carew <[email protected]>
Updated the unit tests to cover both librte_power implementations as well as the external API.
--- app/test/Makefile | 3 +- app/test/autotest_data.py | 26 ++ app/test/test_power.c | 445 +++--------------------------- app/test/test_power_acpi_cpufreq.c | 544 ++++++++++++++++++++++++++++++++++++ app/test/test_power_kvm_vm.c | 308 ++++++++++++++++++++ 5 files changed, 917 insertions(+), 409 deletions(-) create mode 100644 app/test/test_power_acpi_cpufreq.c create mode 100644 app/test/test_power_kvm_vm.c
app/test/Makefile +2 -1
@@ -120,7 +120,8 @@ endif SRCS-$(CONFIG_RTE_LIBRTE_METER) += test_meter.c SRCS-$(CONFIG_RTE_LIBRTE_KNI) += test_kni.c -SRCS-$(CONFIG_RTE_LIBRTE_POWER) += test_power.c +SRCS-$(CONFIG_RTE_LIBRTE_POWER) += test_power.c test_power_acpi_cpufreq.c +SRCS-$(CONFIG_RTE_LIBRTE_POWER) += test_power_kvm_vm.c SRCS-y += test_common.c SRCS-$(CONFIG_RTE_LIBRTE_IVSHMEM) += test_ivshmem.c
app/test/autotest_data.py +26 -0
@@ -425,6 +425,32 @@ non_parallel_test_group_list = [ ] }, { + "Prefix" : "power_acpi_cpufreq", + "Memory" : all_sockets(512), + "Tests" : + [ + { + "Name" : "Power ACPI cpufreq autotest", + "Command" : "power_acpi_cpufreq_autotest", + "Func" : default_autotest, + "Report" : None, + }, + ] +}, +{ + "Prefix" : "power_kvm_vm", + "Memory" : "512", + "Tests" : + [ + { + "Name" : "Power KVM VM autotest", + "Command" : "power_kvm_vm_autotest", + "Func" : default_autotest, + "Report" : None, + }, + ] +}, +{ "Prefix" : "lpm6", "Memory" : "512", "Tests" :
app/test/test_power.c +37 -408
474 lines not on this page · raw message
app/test/test_power_acpi_cpufreq.c new file +544 -0
544 lines not on this page · raw message
app/test/test_power_kvm_vm.c new file +308 -0
308 lines not on this page · raw message
Signature · 1 line
1.7.4.1
Re: [PATCH v6 00/10] Virtual Machine Power Management
In reply to a message not in the archiveHi Pablo and Alan,
2014-11-25 16:18, Pablo de Lara:
Quoted 91 lines
Virtual Machine Power Management.
The following patches add two DPDK sample applications and an alternate implementation of
librte_powerfor use in virtualized environments. The idea is to providelibrte_powerfunctionality from within a VM to address the lack of MSRs to facilitate frequency changes from within a VM. It is ideally suited for Haswell which provides per core frequency scaling.The current
librte_poweraffects frequency changes via the acpi-cpufreq 'userspace' power governor, accessed via sysfs.General Overview:(more information in each patch that follows). The VM Power Management solution provides two components:
1)VM: Allows for the a DPDK application in a VM to reuse the
librte_powerinterface. Each lcore opens a Virto-Serial endpoint channel to the host, where the re-implementation oflibrte_powersimply forwards the requests for frequency change to a host based monitor. The host monitor itself useslibrte_power.
Each lcore channel corresponds to a
serial device '/dev/virtio-ports/virtio.serial.port.poweragent.<lcore_num>' which is opened in non-blocking mode.
While each Virtual CPU can be mapped to multiple physical CPUs it is recommended that each vCPU should be mapped to a single core only.2)Host: The host monitor is managed by a CLI, it allows for adding qemu/KVM virtual machines and associated channels to the monitor, manually changing CPU frequency, inspecting the state of VMs, vCPU to pCPU pinning and managing channels.
Host channel endpoints are Virto-Serial endpoints configured asAF_UNIXfile sockets which follow a specific naming convention
i.e /tmp/powermonitor/<vm_name>.<channel_number>,
each channel has an 1:1 mapping to a VM endpoint
i.e. /dev/virtio-ports/virtio.serial.port.poweragent.<lcore_num>
Host channel endpoints are opened in non-blocking mode and are monitored via epoll. Requests over each channel to change frequency are forwarded to the originallibrte_power.Channels must be manually configured as qemu-kvm command line arguments or libvirt domain definition(xml) e.g.
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</controller>
<channel type='unix'>
<source mode='bind' path='/tmp/powermonitor/<vm_name>.<channel_num>'/> <target type='virtio' name='virtio.serial.port.poweragent.<channel_num>/> <address type='virtio-serial' controller='0' bus='0' port='<N>'/>
</channel>Where multiple channels can be configured by specifying multiple <channel> elements, by replacing <
vm_name>, <channel_num>.
<N>(port number) should be incremented by 1 for each new channel element. More information on Virtio-Serial can be found here:
http://fedoraproject.org/wiki/Features/VirtioSerial
To enable the Hypervisor creation of channels, the host endpoint directory must be created with qemu permissions:
mkdir/tmp/powermonitor
chown qemu:qemu/tmp/powermonitorThe host application runs on two separate lcores:
Core N) CLI: For management of Virtual Machines adding channels to Monitor thread,
inspecting state and manually setting CPU frequency [PATCH 02/09]
Core N+1) Monitor Thread: An epoll based infinite loop that waits on channel events
from VMs and calls the correspondinglibrte_powerfunctions.A sample application is also provided to run on Virtual Machines, this application provides a CLI to manually set the frequency of a vCPU[PATCH 08/09]
The current l3fwd-power sample application can also be run on a VM.
Changes in V6:
Fixed typos and missing some identations and blank linesChanges in V5:
Fixed default target in sample app MakefilesChanges in V4:
Fixed double free of channel during VM shutdown.Changes in V3:
Fixed crash in Guest CLI when host application is not running. Renamed #defines to be more specific to the module they belong Added vCPU pinning via CLIChanges in V2:
Runtime selection oflibrte_powerimplementations. Updated Unit tests to coverlibrte_powerchanges. PATCH[0/3] was sent twice, again as PATCH[0/4] Miscellaneous fixes.
Quoted 11 lines
Alan Carew (10):
Channel Manager and Monitor for VM Power Management(Host). VM Power Management CLI(Host).
CPU Frequency Power Management(Host).
VM Power Management application andMakefile.
VM Power Management CLI(Guest).
VM communication channels for VM Power Management(Guest).librte_powercommon interface for Guest and Host Packet format for VM Power Management(Host and Guest). Build system integration for VM Power Management(Guest and Host) VM Power Management Unit Tests
Thanks to my shiny updated checkpatch, I was able to fix these 2 typos:
WARNING:MISSING_SPACE: break quoted strings at a space character #831: FILE: examples/vm_power_manager/channel_manager.c:722:
+ RTE_LOG(ERR, CHANNEL_MANAGER, "Error connecting to %s, connection"
+ "already established\n", path);
WARNING:MISSING_SPACE: break quoted strings at a space character #1424: FILE: examples/vm_power_manager/channel_monitor.c:181:
+ RTE_LOG(ERR, CHANNEL_MONITOR, "Unable to rte_malloc for"
+ "epoll events\n");
This codebase is really too big to be properly reviewed.
As discussed earlier, it's a workaround for a missing feature in Qemu/KVM. It's now applied in DPDK but it would be really more convenient for everyone if it could be fixed upstream. I hope you'll be able to sustain this work for the goodness of every implied communities.
Thank you
Signature · 1 line
Thomas
PATCH v2 1/1 librte_cmdline: FreeBSD Fix oveflow when size of command result structure is greater than BUFSIZ
In reply to a message not in the archiveWhen using test-pmd with flow director in FreeBSD, the application will segfault/Bus error while parsing the command-line. This is due to how each commands result structure is represented during parsing, where the offsets for each tokens value is stored in a character array(char result_buf[BUFSIZ]) in cmdline_parse()(./lib/librte_cmdline/cmdline_parse.c).
The overflow occurs where BUFSIZ is less than the size of a commands result structure, in this case "struct cmd_pkt_filter_result" (app/test-pmd/cmdline.c) is 1088 bytes and BUFSIZ on FreeBSD is 1024 bytes as opposed to 8192 bytes on Linux.
The problem can be reproduced by running test-pmd on FreeBSD:
./testpmd -c 0x3 -n 4 -- -i --portmask=0x3 --pkt-filter-mode=perfect And adding a filter:add_perfect_filter 0 udp src 192.168.0.0 1024 dst 192.168.0.0 1024 flexbytes 0x800 vlan 0 queue 0 soft 0x17
This patch removes the OS dependency on BUFSIZ and defines and uses a library #define CMDLINE_PARSE_RESULT_BUFSIZE 8192
Added boundary checking to ensure this buffer size cannot overflow, with an error message being produced.
http://git.droids-corp.org/?p=libcmdline.git;a=commitdiff;h=b1d5b169352e57df3fc14c51ffad4b83f3e5613f
--- lib/librte_cmdline/cmdline_parse.c | 22 +++++++++++++++------- lib/librte_cmdline/cmdline_parse.h | 3 +++ 2 files changed, 18 insertions(+), 7 deletions(-)
lib/librte_cmdline/cmdline_parse.c +15 -7
@@ -138,7 +138,7 @@ nb_common_chars(const char * s1, const char * s2)*/ static int match_inst (cmdline_parse_inst_t *inst,const char *buf, -unsigned int nb_match_token,void * result_buf) +unsigned int nb_match_token,void *result_buf,unsigned result_buf_size) {unsigned int token_num=0 ;cmdline_parse_token_hdr_t * token_p;
@@ -162,10 +162,18 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,if (isendofline (*buf) ||iscomment (*buf) )break ; -if (result_buf) +if (result_buf) { +if (token_hdr.offset > result_buf_size) { +printf ("Parse error(%s:%d): Token offset(%u) exceeds maximum " +"size(%u)\n" , __FILE__, __LINE__, token_hdr.offset, + result_buf_size); +return -ENOBUFS ; + } + n = token_hdr.ops->parse (token_p, buf, (char *)result_buf + token_hdr.offset); + }else n = token_hdr.ops->parse (token_p, buf,NULL );
@@ -219,7 +227,7 @@ cmdline_parse(struct cmdline *cl, const char * buf)unsigned int inst_num=0 ;cmdline_parse_inst_t *inst;const char *curbuf; -char result_buf[BUFSIZ ]; +char result_buf[CMDLINE_PARSE_RESULT_BUFSIZE ];void (*f)(void *,struct cmdline *,void *) =NULL ;void *data =NULL ;int comment =0 ;
@@ -280,7 +288,7 @@ cmdline_parse(struct cmdline *cl, const char * buf)debug_printf ("INST %d\n" , inst_num);/* fully parsed */ - tok =match_inst (inst, buf,0 , result_buf); + tok =match_inst (inst, buf,0 , result_buf,sizeof (result_buf));if (tok >0 )/* we matched at least one token */ err =CMDLINE_PARSE_BAD_ARGS ;
@@ -377,10 +385,10 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state, inst = ctx[inst_num];while (inst) {/* parse the first tokens of the inst */ -if (nb_token &&match_inst (inst, buf, nb_token,NULL )) +if (nb_token &&match_inst (inst, buf, nb_token,NULL ,0 )) goto next; -debug_printf ("instruction match \n" ); +debug_printf ("instruction match\n" ); token_p = inst->tokens[nb_token];if (token_p)memcpy (&token_hdr, token_p,sizeof (token_hdr));
@@ -471,7 +479,7 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,/* we need to redo it */ inst = ctx[inst_num]; -if (nb_token &&match_inst (inst, buf, nb_token,NULL )) +if (nb_token &&match_inst (inst, buf, nb_token,NULL ,0 )) goto next2; token_p = inst->tokens[nb_token];
lib/librte_cmdline/cmdline_parse.h +3 -0
@@ -80,6 +80,9 @@ extern "C" {#define CMDLINE_PARSE_COMPLETE_AGAIN 1#define CMDLINE_PARSE_COMPLETED_BUFFER 2 +/* maximum buffer size for parsed result */ +#define CMDLINE_PARSE_RESULT_BUFSIZE 8192 +/** * Stores a pointer to the ops struct, and the offset: the place to * write the parsed result in the destination structure.
Signature · 1 line
1.9.3
A message that is not in the archive (sent off-list or not yet received): 1415611146-32368-1-git-send-email-alan.carew-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org
Re: [PATCH v2] librte_cmdline: FreeBSD Fix oveflow when size of command result structure is greater than BUFSIZ
Hi Alan,
Quoted 20 lines · Alan Carew
When using test-pmd with flow director in FreeBSD, the application will segfault/Bus error while parsing the command-line. This is due to how each commands result structure is represented during parsing, where the offsets for each tokens value is stored in a character
array(char result_buf[BUFSIZ])in cmdline_parse()(./lib/librte_cmdline/cmdline_parse.c).The overflow occurs where BUFSIZ is less than the size of a commands result structure, in this case "struct
cmd_pkt_filter_result" (app/test-pmd/cmdline.c) is 1088 bytes and BUFSIZ on FreeBSD is 1024 bytes as opposed to 8192 bytes on Linux.The problem can be reproduced by running test-pmd on FreeBSD:
./testpmd -c0x3-n 4 -- -i --portmask=0x3 --pkt-filter-mode=perfect And adding a filter:add_perfect_filter0 udp src 192.168.0.0 1024 dst 192.168.0.0 1024 flexbytes0x800vlan 0 queue 0 soft0x17This patch removes the OS dependency on BUFSIZ and defines and uses a library #define
CMDLINE_PARSE_RESULT_BUFSIZE8192
Quoted 7 lines
Added boundary checking to ensure this buffer size cannot overflow, with an error message being produced.
Suggested-by Olivier MATZ <[email protected]>http://git.droids-corp.org/?p=libcmdline.git;a=commitdiff;h=b1d5b169352e57df3fc14c51ffad4b83f3e5613f
Signed-off-by Alan Carew <[email protected]>
I think some checks are missing compared to the original patch. The cmdline_parse_xxx() functions should be modified too. Please see a v3 in my next email.
Regards,
Olivier
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v3 1/1 librte_cmdline: FreeBSD Fix oveflow when size of command result structure is greater than BUFSIZ
From: Alan Carew <[email protected]>
When using test-pmd with flow director in FreeBSD, the application will segfault/Bus error while parsing the command-line. This is due to how each commands result structure is represented during parsing, where the offsets for each tokens value is stored in a character array(char result_buf[BUFSIZ]) in cmdline_parse()(./lib/librte_cmdline/cmdline_parse.c).
The overflow occurs where BUFSIZ is less than the size of a commands result structure, in this case "struct cmd_pkt_filter_result" (app/test-pmd/cmdline.c) is 1088 bytes and BUFSIZ on FreeBSD is 1024 bytes as opposed to 8192 bytes on Linux.
The problem can be reproduced by running test-pmd on FreeBSD:
./testpmd -c 0x3 -n 4 -- -i --portmask=0x3 --pkt-filter-mode=perfect And adding a filter:add_perfect_filter 0 udp src 192.168.0.0 1024 dst 192.168.0.0 1024 flexbytes 0x800 vlan 0 queue 0 soft 0x17
This patch removes the OS dependency on BUFSIZ and defines and uses a library #define CMDLINE_PARSE_RESULT_BUFSIZE 8192
Added boundary checking to ensure this buffer size cannot overflow, with an error message being produced.
http://git.droids-corp.org/?p=libcmdline.git;a=commitdiff;h=b1d5b169352e57df3fc14c51ffad4b83f3e5613f
--- app/test-pmd/parameters.c | 6 +++-- app/test/test_cmdline_etheraddr.c | 13 +++++----- app/test/test_cmdline_ipaddr.c | 27 ++++++++++++-------- app/test/test_cmdline_num.c | 31 +++++++++++++---------- app/test/test_cmdline_portlist.c | 13 +++++----- app/test/test_cmdline_string.c | 13 ++++++---- examples/cmdline/parse_obj_list.c | 6 ++++- examples/cmdline/parse_obj_list.h | 3 ++- examples/vhost_xen/xenstore_parse.c | 5 ++-- lib/librte_cmdline/cmdline_parse.c | 35 ++++++++++++++++--------- lib/librte_cmdline/cmdline_parse.h | 11 +++++--- lib/librte_cmdline/cmdline_parse_etheraddr.c | 5 +++- lib/librte_cmdline/cmdline_parse_etheraddr.h | 4 +-- lib/librte_cmdline/cmdline_parse_ipaddr.c | 6 ++++- lib/librte_cmdline/cmdline_parse_ipaddr.h | 4 +-- lib/librte_cmdline/cmdline_parse_num.c | 38 +++++++++++++++++++++++++++- lib/librte_cmdline/cmdline_parse_num.h | 4 +-- lib/librte_cmdline/cmdline_parse_portlist.c | 5 +++- lib/librte_cmdline/cmdline_parse_portlist.h | 4 +-- lib/librte_cmdline/cmdline_parse_string.c | 6 ++++- lib/librte_cmdline/cmdline_parse_string.h | 2 +- lib/librte_pmd_bond/rte_eth_bond_args.c | 3 ++- 22 files changed, 168 insertions(+), 76 deletions(-)
app/test-pmd/parameters.c +4 -2
@@ -223,7 +223,8 @@ init_peer_eth_addrs(char *config_filename)if (fgets (buf,sizeof (buf), config_file) ==NULL )break ; -if (cmdline_parse_etheraddr (NULL , buf, &peer_eth_addrs[i]) <0 ){ +if (cmdline_parse_etheraddr (NULL , buf, &peer_eth_addrs[i], +sizeof (peer_eth_addrs[i])) <0 ){printf ("Bad MAC address format on line %d\n" , i+1 );fclose (config_file);return -1 ;
@@ -658,7 +659,8 @@ launch_args_parse(int argc, char** argv)"eth-peer: port %d >= RTE_MAX_ETHPORTS(%d)\n" , n, RTE_MAX_ETHPORTS); -if (cmdline_parse_etheraddr (NULL , port_end, &peer_addr) <0 ) +if (cmdline_parse_etheraddr (NULL , port_end, + &peer_addr,sizeof (peer_addr)) <0 )rte_exit (EXIT_FAILURE ,"Invalid ethernet address: %s\n" , port_end);
app/test/test_cmdline_etheraddr.c +7 -6
@@ -130,14 +130,15 @@ test_parse_etheraddr_invalid_param(void)int ret =0 ;/* try all null */ - ret =cmdline_parse_etheraddr (NULL ,NULL ,NULL ); + ret =cmdline_parse_etheraddr (NULL ,NULL ,NULL ,0 );if (ret !=-1 ) {printf ("Error: parser accepted null parameters!\n" );return -1 ; }/* try null buf */ - ret =cmdline_parse_etheraddr (NULL ,NULL , (void *)&result); + ret =cmdline_parse_etheraddr (NULL ,NULL , (void *)&result, +sizeof (result));if (ret !=-1 ) {printf ("Error: parser accepted null string!\n" );return -1 ;
@@ -149,7 +150,7 @@ test_parse_etheraddr_invalid_param(void)snprintf (buf,sizeof (buf),"%s" , ether_addr_valid_strs[0 ].str); - ret =cmdline_parse_etheraddr (NULL , buf,NULL ); + ret =cmdline_parse_etheraddr (NULL , buf,NULL ,0 );if (ret ==-1 ) {printf ("Error: parser rejected null result!\n" );return -1 ;
@@ -185,7 +186,7 @@ test_parse_etheraddr_invalid_data(void)memset (&result,0 ,sizeof (struct ether_addr )); ret =cmdline_parse_etheraddr (NULL , ether_addr_invalid_strs[i], - (void *)&result); + (void *)&result,sizeof (result));if (ret !=-1 ) {printf ("Error: parsing %s succeeded!\n" , ether_addr_invalid_strs[i]);
@@ -210,7 +211,7 @@ test_parse_etheraddr_valid(void)memset (&result,0 ,sizeof (struct ether_addr )); ret =cmdline_parse_etheraddr (NULL , ether_addr_valid_strs[i].str, - (void *)&result); + (void *)&result,sizeof (result));if (ret <0 ) {printf ("Error: parsing %s failed!\n" , ether_addr_valid_strs[i].str);
@@ -229,7 +230,7 @@ test_parse_etheraddr_valid(void)memset (&result,0 ,sizeof (struct ether_addr )); ret =cmdline_parse_etheraddr (NULL , ether_addr_garbage_strs[i], - (void *)&result); + (void *)&result,sizeof (result));if (ret <0 ) {printf ("Error: parsing %s failed!\n" , ether_addr_garbage_strs[i]);
app/test/test_cmdline_ipaddr.c +17 -10
@@ -425,7 +425,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_valid_strs[i].str, (void *)&result); + ipaddr_valid_strs[i].str, (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==
@@ -474,7 +475,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_garbage_addr4_strs[i], (void *)&result); + ipaddr_garbage_addr4_strs[i], (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==
@@ -515,7 +517,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_garbage_addr6_strs[i], (void *)&result); + ipaddr_garbage_addr6_strs[i], (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==
@@ -557,7 +560,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_garbage_network4_strs[i], (void *)&result); + ipaddr_garbage_network4_strs[i], (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==
@@ -598,7 +602,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_garbage_network6_strs[i], (void *)&result); + ipaddr_garbage_network6_strs[i], (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==
@@ -651,7 +656,8 @@ test_parse_ipaddr_invalid_data(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_invalid_strs[i], (void *)&result); + ipaddr_invalid_strs[i], (void *)&result, +sizeof (result));if (ret !=-1 ) { printf("Error: parsing %s as %s succeeded!\n",
@@ -677,25 +683,26 @@ test_parse_ipaddr_invalid_param(void) token.ipaddr_data.flags =CMDLINE_IPADDR_V4 ;/* null token */ -if (cmdline_parse_ipaddr (NULL , buf, (void *)&result) !=-1 ) { +if (cmdline_parse_ipaddr (NULL , buf, (void *)&result, +sizeof (result)) !=-1 ) {printf ("Error: parser accepted invalid parameters!\n" );return -1 ; }/* null buffer */ if (cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, -NULL , (void *)&result) !=-1 ) { +NULL , (void *)&result,sizeof (result)) !=-1 ) {printf ("Error: parser accepted invalid parameters!\n" );return -1 ; }/* empty buffer */ if (cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, -"" , (void *)&result) !=-1 ) { +"" , (void *)&result,sizeof (result)) !=-1 ) {printf ("Error: parser accepted invalid parameters!\n" );return -1 ; }/* null result */ if (cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - buf,NULL ) ==-1 ) { + buf,NULL ,0 ) ==-1 ) {printf ("Error: parser rejected null result!\n" );return -1 ; }
app/test/test_cmdline_num.c +18 -13
@@ -350,14 +350,14 @@ test_parse_num_invalid_param(void) num_valid_positive_strs[0].str);/* try all null */ - ret =cmdline_parse_num (NULL ,NULL ,NULL ); + ret =cmdline_parse_num (NULL ,NULL ,NULL ,0 );if (ret !=-1 ) {printf ("Error: parser accepted null parameters!\n" );return -1 ; }/* try null token */ - ret =cmdline_parse_num (NULL , buf, (void *)&result); + ret =cmdline_parse_num (NULL , buf, (void *)&result,sizeof (result));if (ret !=-1 ) {printf ("Error: parser accepted null token!\n" );return -1 ;
@@ -365,14 +365,15 @@ test_parse_num_invalid_param(void)/* try null buf */ ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *)&token,NULL , - (void *)&result); + (void *)&result,sizeof (result));if (ret !=-1 ) {printf ("Error: parser accepted null string!\n" );return -1 ; }/* try null result */ - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *)&token, buf,NULL ); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *)&token, buf, +NULL ,0 );if (ret ==-1 ) {printf ("Error: parser rejected null result!\n" );return -1 ;
@@ -426,7 +427,7 @@ test_parse_num_invalid_data(void)memset (&buf,0 ,sizeof (buf)); ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *)&token, - num_invalid_strs[i], (void *)&result); + num_invalid_strs[i], (void *)&result,sizeof (result));if (ret !=-1 ) {/* get some info about what we are trying to parse */ cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token,
@@ -466,8 +467,9 @@ test_parse_num_valid(void)cmdline_get_help_num ((cmdline_parse_token_hdr_t *)&token, buf,sizeof (buf)); - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, num_valid_positive_strs[i].str, - (void *)&result); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, + num_valid_positive_strs[i].str, + (void *)&result,sizeof (result));/* if it should have passed but didn't, or if it should have failed but didn't */ if ((ret <0 ) == (can_parse_unsigned (num_valid_positive_strs[i].result, type) >0 )) {
@@ -493,8 +495,9 @@ test_parse_num_valid(void)cmdline_get_help_num ((cmdline_parse_token_hdr_t *)&token, buf,sizeof (buf)); - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, num_valid_negative_strs[i].str, - (void *)&result); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, + num_valid_negative_strs[i].str, + (void *)&result,sizeof (result));/* if it should have passed but didn't, or if it should have failed but didn't */ if ((ret <0 ) == (can_parse_signed (num_valid_negative_strs[i].result, type) >0 )) {
@@ -542,8 +545,9 @@ test_parse_num_valid(void)cmdline_get_help_num ((cmdline_parse_token_hdr_t *)&token, buf,sizeof (buf)); - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, num_garbage_positive_strs[i].str, - (void *)&result); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, + num_garbage_positive_strs[i].str, + (void *)&result,sizeof (result));/* if it should have passed but didn't, or if it should have failed but didn't */ if ((ret <0 ) == (can_parse_unsigned (num_garbage_positive_strs[i].result, type) >0 )) {
@@ -569,8 +573,9 @@ test_parse_num_valid(void)cmdline_get_help_num ((cmdline_parse_token_hdr_t *)&token, buf,sizeof (buf)); - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, num_garbage_negative_strs[i].str, - (void *)&result); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, + num_garbage_negative_strs[i].str, + (void *)&result,sizeof (result));/* if it should have passed but didn't, or if it should have failed but didn't */ if ((ret <0 ) == (can_parse_signed (num_garbage_negative_strs[i].result, type) >0 )) {
app/test/test_cmdline_portlist.c +7 -6
@@ -139,21 +139,22 @@ test_parse_portlist_invalid_param(void)memset (&result,0 ,sizeof (cmdline_portlist_t));/* try all null */ - ret =cmdline_parse_portlist (NULL ,NULL ,NULL ); + ret =cmdline_parse_portlist (NULL ,NULL ,NULL ,0 );if (ret !=-1 ) {printf ("Error: parser accepted null parameters!\n" );return -1 ; }/* try null buf */ - ret =cmdline_parse_portlist (NULL ,NULL , (void *)&result); + ret =cmdline_parse_portlist (NULL ,NULL , (void *)&result, +sizeof (result));if (ret !=-1 ) {printf ("Error: parser accepted null string!\n" );return -1 ; }/* try null result */ - ret =cmdline_parse_portlist (NULL , portlist_valid_strs[0 ].str,NULL ); + ret =cmdline_parse_portlist (NULL , portlist_valid_strs[0 ].str,NULL ,0 );if (ret ==-1 ) {printf ("Error: parser rejected null result!\n" );return -1 ;
@@ -188,7 +189,7 @@ test_parse_portlist_invalid_data(void)memset (&result,0 ,sizeof (cmdline_portlist_t)); ret =cmdline_parse_portlist (NULL , portlist_invalid_strs[i], - (void *)&result); + (void *)&result,sizeof (result));if (ret !=-1 ) {printf ("Error: parsing %s succeeded!\n" , portlist_invalid_strs[i]);
@@ -213,7 +214,7 @@ test_parse_portlist_valid(void)memset (&result,0 ,sizeof (cmdline_portlist_t)); ret =cmdline_parse_portlist (NULL , portlist_valid_strs[i].str, - (void *)&result); + (void *)&result,sizeof (result));if (ret <0 ) {printf ("Error: parsing %s failed!\n" , portlist_valid_strs[i].str);
@@ -232,7 +233,7 @@ test_parse_portlist_valid(void)memset (&result,0 ,sizeof (cmdline_portlist_t)); ret =cmdline_parse_portlist (NULL , portlist_garbage_strs[i], - (void *)&result); + (void *)&result,sizeof (result));if (ret <0 ) {printf ("Error: parsing %s failed!\n" , portlist_garbage_strs[i]);
app/test/test_cmdline_string.c +8 -5
@@ -178,7 +178,7 @@ test_parse_string_invalid_param(void)printf ("Error: function accepted null token!\n" );return -1 ; } -if (cmdline_parse_string (NULL , buf,NULL ) !=-1 ) { +if (cmdline_parse_string (NULL , buf,NULL ,0 ) !=-1 ) {printf ("Error: function accepted null token!\n" );return -1 ; }
@@ -189,7 +189,8 @@ test_parse_string_invalid_param(void)return -1 ; }if (cmdline_parse_string ( - (cmdline_parse_token_hdr_t *)&token,NULL , (void *)&result) !=-1 ) { + (cmdline_parse_token_hdr_t *)&token,NULL , + (void *)&result,sizeof (result)) !=-1 ) {printf ("Error: function accepted null buffer!\n" );return -1 ; }
@@ -200,7 +201,7 @@ test_parse_string_invalid_param(void) }/* test null result */ if (cmdline_parse_string ( - (cmdline_parse_token_hdr_t *)&token, buf,NULL ) ==-1 ) { + (cmdline_parse_token_hdr_t *)&token, buf,NULL ,0 ) ==-1 ) {printf ("Error: function rejected null result!\n" );return -1 ; }
@@ -233,7 +234,8 @@ test_parse_string_invalid_data(void) token.string_data.str = string_invalid_strs[i].fixed_str;if (cmdline_parse_string ((cmdline_parse_token_hdr_t *)&token, - string_invalid_strs[i].str, (void *)buf) !=-1 ) { + string_invalid_strs[i].str, (void *)buf, +sizeof (buf)) !=-1 ) {memset (help_str,0 ,sizeof (help_str));memset (&help_token,0 ,sizeof (help_token));
@@ -330,7 +332,8 @@ test_parse_string_valid(void) token.string_data.str = string_parse_strs[i].fixed_str;if (cmdline_parse_string ((cmdline_parse_token_hdr_t *)&token, - string_parse_strs[i].str, (void *)buf) <0 ) { + string_parse_strs[i].str, (void *)buf, +sizeof (buf)) <0 ) {/* clean help data */ memset (&help_token,0 ,sizeof (help_token));
examples/cmdline/parse_obj_list.c +5 -1
@@ -84,7 +84,8 @@ struct cmdline_token_ops token_obj_list_ops = { };int -parse_obj_list (cmdline_parse_token_hdr_t *tk,const char *buf,void *res) +parse_obj_list (cmdline_parse_token_hdr_t *tk,const char *buf,void *res, +unsigned ressize) {struct token_obj_list *tk2 = (struct token_obj_list *)tk;struct token_obj_list_data *tkd = &tk2->obj_list_data;
@@ -94,6 +95,9 @@ parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)if (*buf ==0 )return -1 ; +if (res && ressize <sizeof (struct object *)) +return -1 ; +while (!cmdline_isendoftoken (buf[token_len])) token_len++;
examples/cmdline/parse_obj_list.h +2 -1
@@ -91,7 +91,8 @@ typedef struct token_obj_list parse_token_obj_list_t;extern struct cmdline_token_ops token_obj_list_ops; -int parse_obj_list (cmdline_parse_token_hdr_t *tk,const char *srcbuf,void *res); +int parse_obj_list (cmdline_parse_token_hdr_t *tk,const char *srcbuf,void *res, +unsigned ressize);int complete_get_nb_obj_list (cmdline_parse_token_hdr_t *tk);int complete_get_elt_obj_list (cmdline_parse_token_hdr_t *tk,int idx,char *dstbuf,unsigned int size);
examples/vhost_xen/xenstore_parse.c +3 -2
@@ -77,7 +77,7 @@ struct grant_node_item { } __attribute__((packed));int cmdline_parse_etheraddr (void *tk,const char *srcbuf, -void *res); +void *res,unsigned ressize);/* Map grant ref refid at addr_ori*/ static void *
@@ -676,7 +676,8 @@ xen_parse_etheraddr(struct xen_vring *vring)if ((buf =xen_read_node (path, &len)) ==NULL ) goto out; -if (cmdline_parse_etheraddr (NULL , buf, &vring->addr) <0 ) +if (cmdline_parse_etheraddr (NULL , buf, &vring->addr, +sizeof (vring->addr)) <0 ) goto out; ret =0 ; out:
lib/librte_cmdline/cmdline_parse.c +23 -12
@@ -138,7 +138,7 @@ nb_common_chars(const char * s1, const char * s2)*/ static int match_inst (cmdline_parse_inst_t *inst,const char *buf, -unsigned int nb_match_token,void * result_buf) +unsigned int nb_match_token,void *resbuf,unsigned resbuf_size) {unsigned int token_num=0 ;cmdline_parse_token_hdr_t * token_p;
@@ -162,12 +162,23 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,if (isendofline (*buf) ||iscomment (*buf) )break ; -if (result_buf) - n = token_hdr.ops->parse (token_p, buf, - (char *)result_buf + - token_hdr.offset); -else - n = token_hdr.ops->parse (token_p, buf,NULL ); +if (resbuf ==NULL ) { + n = token_hdr.ops->parse (token_p, buf,NULL ,0 ); + }else { +unsigned rb_sz; + +if (token_hdr.offset > resbuf_size) { +printf ("Parse error(%s:%d): Token offset(%u) " +"exceeds maximum size(%u)\n" , + __FILE__, __LINE__, + token_hdr.offset, resbuf_size); +return -ENOBUFS ; + } + rb_sz = resbuf_size - token_hdr.offset; + + n = token_hdr.ops->parse (token_p, buf, (char *)resbuf + + token_hdr.offset, rb_sz); + }if (n <0 )break ;
@@ -219,7 +230,7 @@ cmdline_parse(struct cmdline *cl, const char * buf)unsigned int inst_num=0 ;cmdline_parse_inst_t *inst;const char *curbuf; -char result_buf[BUFSIZ ]; +char result_buf[CMDLINE_PARSE_RESULT_BUFSIZE ];void (*f)(void *,struct cmdline *,void *) =NULL ;void *data =NULL ;int comment =0 ;
@@ -280,7 +291,7 @@ cmdline_parse(struct cmdline *cl, const char * buf)debug_printf ("INST %d\n" , inst_num);/* fully parsed */ - tok =match_inst (inst, buf,0 , result_buf); + tok =match_inst (inst, buf,0 , result_buf,sizeof (result_buf));if (tok >0 )/* we matched at least one token */ err =CMDLINE_PARSE_BAD_ARGS ;
@@ -377,10 +388,10 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state, inst = ctx[inst_num];while (inst) {/* parse the first tokens of the inst */ -if (nb_token &&match_inst (inst, buf, nb_token,NULL )) +if (nb_token &&match_inst (inst, buf, nb_token,NULL ,0 )) goto next; -debug_printf ("instruction match \n" ); +debug_printf ("instruction match\n" ); token_p = inst->tokens[nb_token];if (token_p)memcpy (&token_hdr, token_p,sizeof (token_hdr));
@@ -471,7 +482,7 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,/* we need to redo it */ inst = ctx[inst_num]; -if (nb_token &&match_inst (inst, buf, nb_token,NULL )) +if (nb_token &&match_inst (inst, buf, nb_token,NULL ,0 )) goto next2; token_p = inst->tokens[nb_token];
lib/librte_cmdline/cmdline_parse.h +8 -3
@@ -80,6 +80,9 @@ extern "C" {#define CMDLINE_PARSE_COMPLETE_AGAIN 1#define CMDLINE_PARSE_COMPLETED_BUFFER 2 +/* maximum buffer size for parsed result */ +#define CMDLINE_PARSE_RESULT_BUFSIZE 8192 +/** * Stores a pointer to the ops struct, and the offset: the place to * write the parsed result in the destination structure.
@@ -110,12 +113,14 @@ typedef struct cmdline_token_hdr cmdline_parse_token_hdr_t;* -1 on error and 0 on success. */ struct cmdline_token_ops { -/** parse(token ptr, buf, res pts) */ -int (*parse)(cmdline_parse_token_hdr_t *,const char *,void *); +/** parse(token ptr, buf, res pts, buf len) */ +int (*parse)(cmdline_parse_token_hdr_t *,const char *,void *, +unsigned int );/** return the num of possible choices for this token */ int (*complete_get_nb)(cmdline_parse_token_hdr_t *);/** return the elt x for this token (token, idx, dstbuf, size) */ -int (*complete_get_elt)(cmdline_parse_token_hdr_t *,int ,char *,unsigned int ); +int (*complete_get_elt)(cmdline_parse_token_hdr_t *,int ,char *, +unsigned int );/** get help for this token (token, dstbuf, size) */ int (*get_help)(cmdline_parse_token_hdr_t *,char *,unsigned int ); };
lib/librte_cmdline/cmdline_parse_etheraddr.c +4 -1
@@ -137,12 +137,15 @@ my_ether_aton(const char *a)int cmdline_parse_etheraddr (__attribute__((unused))cmdline_parse_token_hdr_t *tk, -const char *buf,void *res) +const char *buf,void *res,unsigned ressize) {unsigned int token_len =0 ;char ether_str[ETHER_ADDRSTRLENLONG +1 ];struct ether_addr *tmp; +if (res && ressize <sizeof (struct ether_addr )) +return -1 ; +if (!buf || ! *buf)return -1 ;
lib/librte_cmdline/cmdline_parse_etheraddr.h +2 -2
@@ -73,9 +73,9 @@ typedef struct cmdline_token_etheraddr cmdline_parse_token_etheraddr_t;extern struct cmdline_token_ops cmdline_token_etheraddr_ops;int cmdline_parse_etheraddr (cmdline_parse_token_hdr_t *tk,const char *srcbuf, -void *res); +void *res,unsigned ressize);int cmdline_get_help_etheraddr (cmdline_parse_token_hdr_t *tk,char *dstbuf, -unsigned int size); +unsigned int size);#define TOKEN_ETHERADDR_INITIALIZER (structure, field) \ { \
lib/librte_cmdline/cmdline_parse_ipaddr.c +5 -1
@@ -306,7 +306,8 @@ inet_pton6(const char *src, unsigned char *dst) }int -cmdline_parse_ipaddr (cmdline_parse_token_hdr_t *tk,const char *buf,void *res) +cmdline_parse_ipaddr (cmdline_parse_token_hdr_t *tk,const char *buf,void *res, +unsigned ressize) {struct cmdline_token_ipaddr *tk2;unsigned int token_len =0 ;
@@ -315,6 +316,9 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)char *prefix, *prefix_end;long prefixlen =0 ; +if (res && ressize <sizeof (cmdline_ipaddr_t)) +return -1 ; +if (!buf || !tk || ! *buf)return -1 ;
lib/librte_cmdline/cmdline_parse_ipaddr.h +2 -2
@@ -92,9 +92,9 @@ typedef struct cmdline_token_ipaddr cmdline_parse_token_ipaddr_t;extern struct cmdline_token_ops cmdline_token_ipaddr_ops;int cmdline_parse_ipaddr (cmdline_parse_token_hdr_t *tk,const char *srcbuf, -void *res); +void *res,unsigned ressize);int cmdline_get_help_ipaddr (cmdline_parse_token_hdr_t *tk,char *dstbuf, -unsigned int size); +unsigned int size);#define TOKEN_IPADDR_INITIALIZER (structure, field) \ { \
lib/librte_cmdline/cmdline_parse_num.c +37 -1
@@ -119,10 +119,40 @@ add_to_res(unsigned int c, uint64_t *res, unsigned int base)return 0 ; } +static int +check_res_size (struct cmdline_token_num_data *nd,unsigned ressize) +{ +switch (nd->type) { +case INT8 : +case UINT8 : +if (ressize <sizeof (int8_t )) +return -1 ; +break ; +case INT16 : +case UINT16 : +if (ressize <sizeof (int16_t )) +return -1 ; +break ; +case INT32 : +case UINT32 : +if (ressize <sizeof (int32_t )) +return -1 ; +break ; +case INT64 : +case UINT64 : +if (ressize <sizeof (int64_t )) +return -1 ; +break ; +default : +return -1 ; + } +return 0 ; +}/* parse an int */ int -cmdline_parse_num (cmdline_parse_token_hdr_t *tk,const char *srcbuf,void *res) +cmdline_parse_num (cmdline_parse_token_hdr_t *tk,const char *srcbuf,void *res, +unsigned ressize) {struct cmdline_token_num_data nd;enum num_parse_state_t st =START ;
@@ -141,6 +171,12 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)memcpy (&nd, &((struct cmdline_token_num *)tk)->num_data,sizeof (nd)); +/* check that we have enough room in res */ +if (res) { +if (check_res_size (&nd, ressize) <0 ) +return -1 ; + } +while ( st !=ERROR && c && !cmdline_isendoftoken (c) ) {debug_printf ("%c %x -> " , c, c);switch (st) {
lib/librte_cmdline/cmdline_parse_num.h +2 -2
@@ -89,9 +89,9 @@ typedef struct cmdline_token_num cmdline_parse_token_num_t;extern struct cmdline_token_ops cmdline_token_num_ops;int cmdline_parse_num (cmdline_parse_token_hdr_t *tk, -const char *srcbuf,void *res); +const char *srcbuf,void *res,unsigned ressize);int cmdline_get_help_num (cmdline_parse_token_hdr_t *tk, -char *dstbuf,unsigned int size); +char *dstbuf,unsigned int size);#define TOKEN_NUM_INITIALIZER (structure, field, numtype) \ { \
lib/librte_cmdline/cmdline_parse_portlist.c +4 -1
@@ -127,7 +127,7 @@ parse_ports(cmdline_portlist_t * pl, const char * str)int cmdline_parse_portlist (__attribute__((unused))cmdline_parse_token_hdr_t *tk, -const char *buf,void *res) +const char *buf,void *res,unsigned ressize) {unsigned int token_len =0 ;char portlist_str[PORTLIST_TOKEN_SIZE +1 ];
@@ -136,6 +136,9 @@ cmdline_parse_portlist(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,if (!buf || ! *buf)return (-1 ); +if (res && ressize <PORTLIST_TOKEN_SIZE ) +return -1 ; + pl = res; while (!cmdline_isendoftoken(buf[token_len]) &&
lib/librte_cmdline/cmdline_parse_portlist.h +2 -2
@@ -81,9 +81,9 @@ typedef struct cmdline_token_portlist cmdline_parse_token_portlist_t;extern struct cmdline_token_ops cmdline_token_portlist_ops;int cmdline_parse_portlist (cmdline_parse_token_hdr_t *tk, -const char *srcbuf,void *res); +const char *srcbuf,void *res,unsigned ressize);int cmdline_get_help_portlist (cmdline_parse_token_hdr_t *tk, -char *dstbuf,unsigned int size); +char *dstbuf,unsigned int size);#define TOKEN_PORTLIST_INITIALIZER (structure, field) \ { \
lib/librte_cmdline/cmdline_parse_string.c +5 -1
@@ -105,13 +105,17 @@ get_next_token(const char *s) }int -cmdline_parse_string (cmdline_parse_token_hdr_t *tk,const char *buf,void *res) +cmdline_parse_string (cmdline_parse_token_hdr_t *tk,const char *buf,void *res, +unsigned ressize) {struct cmdline_token_string *tk2;struct cmdline_token_string_data *sd;unsigned int token_len;const char *str; +if (res && ressize <STR_TOKEN_SIZE ) +return -1 ; +if (!tk || !buf || ! *buf)return -1 ;
lib/librte_cmdline/cmdline_parse_string.h +1 -1
@@ -83,7 +83,7 @@ typedef struct cmdline_token_string cmdline_parse_token_string_t;extern struct cmdline_token_ops cmdline_token_string_ops;int cmdline_parse_string (cmdline_parse_token_hdr_t *tk,const char *srcbuf, -void *res); +void *res,unsigned ressize);int cmdline_complete_get_nb_string (cmdline_parse_token_hdr_t *tk);int cmdline_complete_get_elt_string (cmdline_parse_token_hdr_t *tk,int idx,char *dstbuf,unsigned int size);
lib/librte_pmd_bond/rte_eth_bond_args.c +2 -1
@@ -254,7 +254,8 @@ bond_ethdev_parse_bond_mac_addr_kvarg(const char *key __rte_unused,return -1 ;/* Parse MAC */ -return cmdline_parse_etheraddr (NULL , value, extra_args); +return cmdline_parse_etheraddr (NULL , value, extra_args, +sizeof (struct ether_addr )); }int
Signature · 1 line
2.1.0
A message that is not in the archive (sent off-list or not yet received): 1417789147-5636-1-git-send-email-olivier.matz-pdR9zngts4EAvxtiuMwx3w@public.gmane.org
Bruce Richardson
Tested-byRe: [PATCH v3] librte_cmdline: FreeBSD Fix oveflow when size of command result structure is greater than BUFSIZ
Quoted 22 lines · Olivier Matz
From: Alan Carew <[email protected]>
When using test-pmd with flow director in FreeBSD, the application will segfault/Bus error while parsing the command-line. This is due to how each commands result structure is represented during parsing, where the offsets for each tokens value is stored in a character
array(char result_buf[BUFSIZ])in cmdline_parse()(./lib/librte_cmdline/cmdline_parse.c).The overflow occurs where BUFSIZ is less than the size of a commands result structure, in this case "struct
cmd_pkt_filter_result" (app/test-pmd/cmdline.c) is 1088 bytes and BUFSIZ on FreeBSD is 1024 bytes as opposed to 8192 bytes on Linux.The problem can be reproduced by running test-pmd on FreeBSD:
./testpmd -c0x3-n 4 -- -i --portmask=0x3 --pkt-filter-mode=perfect And adding a filter:add_perfect_filter0 udp src 192.168.0.0 1024 dst 192.168.0.0 1024 flexbytes0x800vlan 0 queue 0 soft0x17This patch removes the OS dependency on BUFSIZ and defines and uses a library #define
CMDLINE_PARSE_RESULT_BUFSIZE8192
Quoted 8 lines
Added boundary checking to ensure this buffer size cannot overflow, with an error message being produced.
Suggested-by Olivier MATZ <[email protected]>http://git.droids-corp.org/?p=libcmdline.git;a=commitdiff;h=b1d5b169352e57df3fc14c51ffad4b83f3e5613f
Signed-off-by Alan Carew <[email protected]>Signed-off-by Olivier MATZ <[email protected]>
Tested on FreeBSD 10 and this patch fixes the issue described.
Quoted 827 lines
--- app/test-pmd/parameters.c | 6 +++-- app/test/test_cmdline_etheraddr.c | 13 +++++----- app/test/test_cmdline_ipaddr.c | 27 ++++++++++++-------- app/test/test_cmdline_num.c | 31 +++++++++++++---------- app/test/test_cmdline_portlist.c | 13 +++++----- app/test/test_cmdline_string.c | 13 ++++++---- examples/cmdline/parse_obj_list.c | 6 ++++- examples/cmdline/parse_obj_list.h | 3 ++- examples/vhost_xen/xenstore_parse.c | 5 ++-- lib/librte_cmdline/cmdline_parse.c | 35 ++++++++++++++++--------- lib/librte_cmdline/cmdline_parse.h | 11 +++++--- lib/librte_cmdline/cmdline_parse_etheraddr.c | 5 +++- lib/librte_cmdline/cmdline_parse_etheraddr.h | 4 +-- lib/librte_cmdline/cmdline_parse_ipaddr.c | 6 ++++- lib/librte_cmdline/cmdline_parse_ipaddr.h | 4 +-- lib/librte_cmdline/cmdline_parse_num.c | 38 +++++++++++++++++++++++++++- lib/librte_cmdline/cmdline_parse_num.h | 4 +-- lib/librte_cmdline/cmdline_parse_portlist.c | 5 +++- lib/librte_cmdline/cmdline_parse_portlist.h | 4 +-- lib/librte_cmdline/cmdline_parse_string.c | 6 ++++- lib/librte_cmdline/cmdline_parse_string.h | 2 +- lib/librte_pmd_bond/rte_eth_bond_args.c | 3 ++- 22 files changed, 168 insertions(+), 76 deletions(-)app/test-pmd/parameters.c +4
-2@@ -223,7 +223,8 @@ init_peer_eth_addrs(char *config_filename)if (fgets (buf,sizeof (buf), config_file) ==NULL )break ; -if (cmdline_parse_etheraddr (NULL , buf, &peer_eth_addrs[i]) <0 ){ +if (cmdline_parse_etheraddr (NULL , buf, &peer_eth_addrs[i], +sizeof (peer_eth_addrs[i])) <0 ){printf ("Bad MAC address format on line %d\n" , i+1 );fclose (config_file);return -1 ;@@ -658,7 +659,8 @@ launch_args_parse(int argc, char** argv)"eth-peer: port %d >= RTE_MAX_ETHPORTS(%d)\n" , n, RTE_MAX_ETHPORTS); -if (cmdline_parse_etheraddr (NULL , port_end, &peer_addr) <0 ) +if (cmdline_parse_etheraddr (NULL , port_end, + &peer_addr,sizeof (peer_addr)) <0 )rte_exit (EXIT_FAILURE ,"Invalid ethernet address: %s\n" , port_end);app/test/test_cmdline_etheraddr.c +7
-6@@ -130,14 +130,15 @@ test_parse_etheraddr_invalid_param(void)int ret =0 ;/* try all null */ - ret =cmdline_parse_etheraddr (NULL ,NULL ,NULL ); + ret =cmdline_parse_etheraddr (NULL ,NULL ,NULL ,0 );if (ret !=-1 ) {printf ("Error: parser accepted null parameters!\n" );return -1 ; }/* try null buf */ - ret =cmdline_parse_etheraddr (NULL ,NULL , (void *)&result); + ret =cmdline_parse_etheraddr (NULL ,NULL , (void *)&result, +sizeof (result));if (ret !=-1 ) {printf ("Error: parser accepted null string!\n" );return -1 ;@@ -149,7 +150,7 @@ test_parse_etheraddr_invalid_param(void)snprintf (buf,sizeof (buf),"%s" , ether_addr_valid_strs[0 ].str); - ret =cmdline_parse_etheraddr (NULL , buf,NULL ); + ret =cmdline_parse_etheraddr (NULL , buf,NULL ,0 );if (ret ==-1 ) {printf ("Error: parser rejected null result!\n" );return -1 ;@@ -185,7 +186,7 @@ test_parse_etheraddr_invalid_data(void)memset (&result,0 ,sizeof (struct ether_addr )); ret =cmdline_parse_etheraddr (NULL , ether_addr_invalid_strs[i], - (void *)&result); + (void *)&result,sizeof (result));if (ret !=-1 ) {printf ("Error: parsing %s succeeded!\n" , ether_addr_invalid_strs[i]);@@ -210,7 +211,7 @@ test_parse_etheraddr_valid(void)memset (&result,0 ,sizeof (struct ether_addr )); ret =cmdline_parse_etheraddr (NULL , ether_addr_valid_strs[i].str, - (void *)&result); + (void *)&result,sizeof (result));if (ret <0 ) {printf ("Error: parsing %s failed!\n" , ether_addr_valid_strs[i].str);@@ -229,7 +230,7 @@ test_parse_etheraddr_valid(void)memset (&result,0 ,sizeof (struct ether_addr )); ret =cmdline_parse_etheraddr (NULL , ether_addr_garbage_strs[i], - (void *)&result); + (void *)&result,sizeof (result));if (ret <0 ) {printf ("Error: parsing %s failed!\n" , ether_addr_garbage_strs[i]);app/test/test_cmdline_ipaddr.c +17
-10@@ -425,7 +425,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_valid_strs[i].str, (void *)&result); + ipaddr_valid_strs[i].str, (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==@@ -474,7 +475,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_garbage_addr4_strs[i], (void *)&result); + ipaddr_garbage_addr4_strs[i], (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==@@ -515,7 +517,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_garbage_addr6_strs[i], (void *)&result); + ipaddr_garbage_addr6_strs[i], (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==@@ -557,7 +560,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_garbage_network4_strs[i], (void *)&result); + ipaddr_garbage_network4_strs[i], (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==@@ -598,7 +602,8 @@ test_parse_ipaddr_valid(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_garbage_network6_strs[i], (void *)&result); + ipaddr_garbage_network6_strs[i], (void *)&result, +sizeof (result));/* if should have passed, or should have failed */ if ((ret < 0) ==@@ -651,7 +656,8 @@ test_parse_ipaddr_invalid_data(void) buf, sizeof(buf)); ret =cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - ipaddr_invalid_strs[i], (void *)&result); + ipaddr_invalid_strs[i], (void *)&result, +sizeof (result));if (ret !=-1 ) { printf("Error: parsing %s as %s succeeded!\n",@@ -677,25 +683,26 @@ test_parse_ipaddr_invalid_param(void) token.ipaddr_data.flags =CMDLINE_IPADDR_V4 ;/* null token */ -if (cmdline_parse_ipaddr (NULL , buf, (void *)&result) !=-1 ) { +if (cmdline_parse_ipaddr (NULL , buf, (void *)&result, +sizeof (result)) !=-1 ) {printf ("Error: parser accepted invalid parameters!\n" );return -1 ; }/* null buffer */ if (cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, -NULL , (void *)&result) !=-1 ) { +NULL , (void *)&result,sizeof (result)) !=-1 ) {printf ("Error: parser accepted invalid parameters!\n" );return -1 ; }/* empty buffer */ if (cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, -"" , (void *)&result) !=-1 ) { +"" , (void *)&result,sizeof (result)) !=-1 ) {printf ("Error: parser accepted invalid parameters!\n" );return -1 ; }/* null result */ if (cmdline_parse_ipaddr ((cmdline_parse_token_hdr_t *)&token, - buf,NULL ) ==-1 ) { + buf,NULL ,0 ) ==-1 ) {printf ("Error: parser rejected null result!\n" );return -1 ; }app/test/test_cmdline_num.c +18
-13@@ -350,14 +350,14 @@ test_parse_num_invalid_param(void) num_valid_positive_strs[0].str);/* try all null */ - ret =cmdline_parse_num (NULL ,NULL ,NULL ); + ret =cmdline_parse_num (NULL ,NULL ,NULL ,0 );if (ret !=-1 ) {printf ("Error: parser accepted null parameters!\n" );return -1 ; }/* try null token */ - ret =cmdline_parse_num (NULL , buf, (void *)&result); + ret =cmdline_parse_num (NULL , buf, (void *)&result,sizeof (result));if (ret !=-1 ) {printf ("Error: parser accepted null token!\n" );return -1 ;@@ -365,14 +365,15 @@ test_parse_num_invalid_param(void)/* try null buf */ ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *)&token,NULL , - (void *)&result); + (void *)&result,sizeof (result));if (ret !=-1 ) {printf ("Error: parser accepted null string!\n" );return -1 ; }/* try null result */ - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *)&token, buf,NULL ); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *)&token, buf, +NULL ,0 );if (ret ==-1 ) {printf ("Error: parser rejected null result!\n" );return -1 ;@@ -426,7 +427,7 @@ test_parse_num_invalid_data(void)memset (&buf,0 ,sizeof (buf)); ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *)&token, - num_invalid_strs[i], (void *)&result); + num_invalid_strs[i], (void *)&result,sizeof (result));if (ret !=-1 ) {/* get some info about what we are trying to parse */ cmdline_get_help_num((cmdline_parse_token_hdr_t*)&token,@@ -466,8 +467,9 @@ test_parse_num_valid(void)cmdline_get_help_num ((cmdline_parse_token_hdr_t *)&token, buf,sizeof (buf)); - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, num_valid_positive_strs[i].str, - (void *)&result); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, + num_valid_positive_strs[i].str, + (void *)&result,sizeof (result));/* if it should have passed but didn't, or if it should have failed but didn't */ if ((ret <0 ) == (can_parse_unsigned (num_valid_positive_strs[i].result, type) >0 )) {@@ -493,8 +495,9 @@ test_parse_num_valid(void)cmdline_get_help_num ((cmdline_parse_token_hdr_t *)&token, buf,sizeof (buf)); - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, num_valid_negative_strs[i].str, - (void *)&result); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, + num_valid_negative_strs[i].str, + (void *)&result,sizeof (result));/* if it should have passed but didn't, or if it should have failed but didn't */ if ((ret <0 ) == (can_parse_signed (num_valid_negative_strs[i].result, type) >0 )) {@@ -542,8 +545,9 @@ test_parse_num_valid(void)cmdline_get_help_num ((cmdline_parse_token_hdr_t *)&token, buf,sizeof (buf)); - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, num_garbage_positive_strs[i].str, - (void *)&result); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, + num_garbage_positive_strs[i].str, + (void *)&result,sizeof (result));/* if it should have passed but didn't, or if it should have failed but didn't */ if ((ret <0 ) == (can_parse_unsigned (num_garbage_positive_strs[i].result, type) >0 )) {@@ -569,8 +573,9 @@ test_parse_num_valid(void)cmdline_get_help_num ((cmdline_parse_token_hdr_t *)&token, buf,sizeof (buf)); - ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, num_garbage_negative_strs[i].str, - (void *)&result); + ret =cmdline_parse_num ((cmdline_parse_token_hdr_t *) &token, + num_garbage_negative_strs[i].str, + (void *)&result,sizeof (result));/* if it should have passed but didn't, or if it should have failed but didn't */ if ((ret <0 ) == (can_parse_signed (num_garbage_negative_strs[i].result, type) >0 )) {app/test/test_cmdline_portlist.c +7
-6@@ -139,21 +139,22 @@ test_parse_portlist_invalid_param(void)memset (&result,0 ,sizeof (cmdline_portlist_t));/* try all null */ - ret =cmdline_parse_portlist (NULL ,NULL ,NULL ); + ret =cmdline_parse_portlist (NULL ,NULL ,NULL ,0 );if (ret !=-1 ) {printf ("Error: parser accepted null parameters!\n" );return -1 ; }/* try null buf */ - ret =cmdline_parse_portlist (NULL ,NULL , (void *)&result); + ret =cmdline_parse_portlist (NULL ,NULL , (void *)&result, +sizeof (result));if (ret !=-1 ) {printf ("Error: parser accepted null string!\n" );return -1 ; }/* try null result */ - ret =cmdline_parse_portlist (NULL , portlist_valid_strs[0 ].str,NULL ); + ret =cmdline_parse_portlist (NULL , portlist_valid_strs[0 ].str,NULL ,0 );if (ret ==-1 ) {printf ("Error: parser rejected null result!\n" );return -1 ;@@ -188,7 +189,7 @@ test_parse_portlist_invalid_data(void)memset (&result,0 ,sizeof (cmdline_portlist_t)); ret =cmdline_parse_portlist (NULL , portlist_invalid_strs[i], - (void *)&result); + (void *)&result,sizeof (result));if (ret !=-1 ) {printf ("Error: parsing %s succeeded!\n" , portlist_invalid_strs[i]);@@ -213,7 +214,7 @@ test_parse_portlist_valid(void)memset (&result,0 ,sizeof (cmdline_portlist_t)); ret =cmdline_parse_portlist (NULL , portlist_valid_strs[i].str, - (void *)&result); + (void *)&result,sizeof (result));if (ret <0 ) {printf ("Error: parsing %s failed!\n" , portlist_valid_strs[i].str);@@ -232,7 +233,7 @@ test_parse_portlist_valid(void)memset (&result,0 ,sizeof (cmdline_portlist_t)); ret =cmdline_parse_portlist (NULL , portlist_garbage_strs[i], - (void *)&result); + (void *)&result,sizeof (result));if (ret <0 ) {printf ("Error: parsing %s failed!\n" , portlist_garbage_strs[i]);app/test/test_cmdline_string.c +8
-5@@ -178,7 +178,7 @@ test_parse_string_invalid_param(void)printf ("Error: function accepted null token!\n" );return -1 ; } -if (cmdline_parse_string (NULL , buf,NULL ) !=-1 ) { +if (cmdline_parse_string (NULL , buf,NULL ,0 ) !=-1 ) {printf ("Error: function accepted null token!\n" );return -1 ; }@@ -189,7 +189,8 @@ test_parse_string_invalid_param(void)return -1 ; }if (cmdline_parse_string ( - (cmdline_parse_token_hdr_t *)&token,NULL , (void *)&result) !=-1 ) { + (cmdline_parse_token_hdr_t *)&token,NULL , + (void *)&result,sizeof (result)) !=-1 ) {printf ("Error: function accepted null buffer!\n" );return -1 ; }@@ -200,7 +201,7 @@ test_parse_string_invalid_param(void) }/* test null result */ if (cmdline_parse_string ( - (cmdline_parse_token_hdr_t *)&token, buf,NULL ) ==-1 ) { + (cmdline_parse_token_hdr_t *)&token, buf,NULL ,0 ) ==-1 ) {printf ("Error: function rejected null result!\n" );return -1 ; }@@ -233,7 +234,8 @@ test_parse_string_invalid_data(void) token.string_data.str = string_invalid_strs[i].fixed_str;if (cmdline_parse_string ((cmdline_parse_token_hdr_t *)&token, - string_invalid_strs[i].str, (void *)buf) !=-1 ) { + string_invalid_strs[i].str, (void *)buf, +sizeof (buf)) !=-1 ) {memset (help_str,0 ,sizeof (help_str));memset (&help_token,0 ,sizeof (help_token));@@ -330,7 +332,8 @@ test_parse_string_valid(void) token.string_data.str = string_parse_strs[i].fixed_str;if (cmdline_parse_string ((cmdline_parse_token_hdr_t *)&token, - string_parse_strs[i].str, (void *)buf) <0 ) { + string_parse_strs[i].str, (void *)buf, +sizeof (buf)) <0 ) {/* clean help data */ memset (&help_token,0 ,sizeof (help_token));examples/cmdline/parse_obj_list.c +5
-1@@ -84,7 +84,8 @@ struct cmdline_token_ops token_obj_list_ops = { };int -parse_obj_list (cmdline_parse_token_hdr_t *tk,const char *buf,void *res) +parse_obj_list (cmdline_parse_token_hdr_t *tk,const char *buf,void *res, +unsigned ressize) {struct token_obj_list *tk2 = (struct token_obj_list *)tk;struct token_obj_list_data *tkd = &tk2->obj_list_data;@@ -94,6 +95,9 @@ parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)if (*buf ==0 )return -1 ; +if (res && ressize <sizeof (struct object *)) +return -1 ; +while (!cmdline_isendoftoken (buf[token_len])) token_len++;examples/cmdline/parse_obj_list.h +2
-1@@ -91,7 +91,8 @@ typedef struct token_obj_list parse_token_obj_list_t;extern struct cmdline_token_ops token_obj_list_ops; -int parse_obj_list (cmdline_parse_token_hdr_t *tk,const char *srcbuf,void *res); +int parse_obj_list (cmdline_parse_token_hdr_t *tk,const char *srcbuf,void *res, +unsigned ressize);int complete_get_nb_obj_list (cmdline_parse_token_hdr_t *tk);int complete_get_elt_obj_list (cmdline_parse_token_hdr_t *tk,int idx,char *dstbuf,unsigned int size);examples/vhost_xen/xenstore_parse.c +3
-2@@ -77,7 +77,7 @@ struct grant_node_item { } __attribute__((packed));int cmdline_parse_etheraddr (void *tk,const char *srcbuf, -void *res); +void *res,unsigned ressize);/* Map grant ref refid at addr_ori*/ static void *@@ -676,7 +676,8 @@ xen_parse_etheraddr(struct xen_vring *vring)if ((buf =xen_read_node (path, &len)) ==NULL ) goto out; -if (cmdline_parse_etheraddr (NULL , buf, &vring->addr) <0 ) +if (cmdline_parse_etheraddr (NULL , buf, &vring->addr, +sizeof (vring->addr)) <0 ) goto out; ret =0 ; out:lib/librte_cmdline/cmdline_parse.c +23
-12@@ -138,7 +138,7 @@ nb_common_chars(const char * s1, const char * s2)*/ static int match_inst (cmdline_parse_inst_t *inst,const char *buf, -unsigned int nb_match_token,void * result_buf) +unsigned int nb_match_token,void *resbuf,unsigned resbuf_size) {unsigned int token_num=0 ;cmdline_parse_token_hdr_t * token_p;@@ -162,12 +162,23 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,if (isendofline (*buf) ||iscomment (*buf) )break ; -if (result_buf) - n = token_hdr.ops->parse (token_p, buf, - (char *)result_buf + - token_hdr.offset); -else - n = token_hdr.ops->parse (token_p, buf,NULL ); +if (resbuf ==NULL ) { + n = token_hdr.ops->parse (token_p, buf,NULL ,0 ); + }else { +unsigned rb_sz; + +if (token_hdr.offset > resbuf_size) { +printf ("Parse error(%s:%d): Token offset(%u) " +"exceeds maximum size(%u)\n" , + __FILE__, __LINE__, + token_hdr.offset, resbuf_size); +return -ENOBUFS ; + } + rb_sz = resbuf_size - token_hdr.offset; + + n = token_hdr.ops->parse (token_p, buf, (char *)resbuf + + token_hdr.offset, rb_sz); + }if (n <0 )break ;@@ -219,7 +230,7 @@ cmdline_parse(struct cmdline *cl, const char * buf)unsigned int inst_num=0 ;cmdline_parse_inst_t *inst;const char *curbuf; -char result_buf[BUFSIZ ]; +char result_buf[CMDLINE_PARSE_RESULT_BUFSIZE ];void (*f)(void *,struct cmdline *,void *) =NULL ;void *data =NULL ;int comment =0 ;@@ -280,7 +291,7 @@ cmdline_parse(struct cmdline *cl, const char * buf)debug_printf ("INST %d\n" , inst_num);/* fully parsed */ - tok =match_inst (inst, buf,0 , result_buf); + tok =match_inst (inst, buf,0 , result_buf,sizeof (result_buf));if (tok >0 )/* we matched at least one token */ err =CMDLINE_PARSE_BAD_ARGS ;@@ -377,10 +388,10 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state, inst = ctx[inst_num];while (inst) {/* parse the first tokens of the inst */ -if (nb_token &&match_inst (inst, buf, nb_token,NULL )) +if (nb_token &&match_inst (inst, buf, nb_token,NULL ,0 )) goto next; -debug_printf ("instruction match \n" ); +debug_printf ("instruction match\n" ); token_p = inst->tokens[nb_token];if (token_p)memcpy (&token_hdr, token_p,sizeof (token_hdr));@@ -471,7 +482,7 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,/* we need to redo it */ inst = ctx[inst_num]; -if (nb_token &&match_inst (inst, buf, nb_token,NULL )) +if (nb_token &&match_inst (inst, buf, nb_token,NULL ,0 )) goto next2; token_p = inst->tokens[nb_token];lib/librte_cmdline/cmdline_parse.h +8
-3@@ -80,6 +80,9 @@ extern "C" {#define CMDLINE_PARSE_COMPLETE_AGAIN 1#define CMDLINE_PARSE_COMPLETED_BUFFER 2 +/* maximum buffer size for parsed result */ +#define CMDLINE_PARSE_RESULT_BUFSIZE 8192 +/** * Stores a pointer to the ops struct, and the offset: the place to * write the parsed result in the destination structure. @@ -110,12 +113,14 @@ typedef struct cmdline_token_hdr cmdline_parse_token_hdr_t;* -1 on error and 0 on success. */ struct cmdline_token_ops { -/** parse(token ptr, buf, res pts) */ -int (*parse)(cmdline_parse_token_hdr_t *,const char *,void *); +/** parse(token ptr, buf, res pts, buf len) */ +int (*parse)(cmdline_parse_token_hdr_t *,const char *,void *, +unsigned int );/** return the num of possible choices for this token */ int (*complete_get_nb)(cmdline_parse_token_hdr_t *);/** return the elt x for this token (token, idx, dstbuf, size) */ -int (*complete_get_elt)(cmdline_parse_token_hdr_t *,int ,char *,unsigned int ); +int (*complete_get_elt)(cmdline_parse_token_hdr_t *,int ,char *, +unsigned int );/** get help for this token (token, dstbuf, size) */ int (*get_help)(cmdline_parse_token_hdr_t *,char *,unsigned int ); };lib/librte_cmdline/cmdline_parse_etheraddr.c +4
-1@@ -137,12 +137,15 @@ my_ether_aton(const char *a)int cmdline_parse_etheraddr (__attribute__((unused))cmdline_parse_token_hdr_t *tk, -const char *buf,void *res) +const char *buf,void *res,unsigned ressize) {unsigned int token_len =0 ;char ether_str[ETHER_ADDRSTRLENLONG +1 ];struct ether_addr *tmp; +if (res && ressize <sizeof (struct ether_addr )) +return -1 ; +if (!buf || ! *buf)return -1 ;lib/librte_cmdline/cmdline_parse_etheraddr.h +2
-2@@ -73,9 +73,9 @@ typedef struct cmdline_token_etheraddr cmdline_parse_token_etheraddr_t;extern struct cmdline_token_ops cmdline_token_etheraddr_ops;int cmdline_parse_etheraddr (cmdline_parse_token_hdr_t *tk,const char *srcbuf, -void *res); +void *res,unsigned ressize);int cmdline_get_help_etheraddr (cmdline_parse_token_hdr_t *tk,char *dstbuf, -unsigned int size); +unsigned int size);#define TOKEN_ETHERADDR_INITIALIZER (structure, field) \ { \lib/librte_cmdline/cmdline_parse_ipaddr.c +5
-1@@ -306,7 +306,8 @@ inet_pton6(const char *src, unsigned char *dst) }int -cmdline_parse_ipaddr (cmdline_parse_token_hdr_t *tk,const char *buf,void *res) +cmdline_parse_ipaddr (cmdline_parse_token_hdr_t *tk,const char *buf,void *res, +unsigned ressize) {struct cmdline_token_ipaddr *tk2;unsigned int token_len =0 ;@@ -315,6 +316,9 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)char *prefix, *prefix_end;long prefixlen =0 ; +if (res && ressize <sizeof (cmdline_ipaddr_t)) +return -1 ; +if (!buf || !tk || ! *buf)return -1 ;lib/librte_cmdline/cmdline_parse_ipaddr.h +2
-2@@ -92,9 +92,9 @@ typedef struct cmdline_token_ipaddr cmdline_parse_token_ipaddr_t;extern struct cmdline_token_ops cmdline_token_ipaddr_ops;int cmdline_parse_ipaddr (cmdline_parse_token_hdr_t *tk,const char *srcbuf, -void *res); +void *res,unsigned ressize);int cmdline_get_help_ipaddr (cmdline_parse_token_hdr_t *tk,char *dstbuf, -unsigned int size); +unsigned int size);#define TOKEN_IPADDR_INITIALIZER (structure, field) \ { \lib/librte_cmdline/cmdline_parse_num.c +37
-1@@ -119,10 +119,40 @@ add_to_res(unsigned int c, uint64_t *res, unsigned int base)return 0 ; } +static int +check_res_size (struct cmdline_token_num_data *nd,unsigned ressize) +{ +switch (nd->type) { +case INT8 : +case UINT8 : +if (ressize <sizeof (int8_t )) +return -1 ; +break ; +case INT16 : +case UINT16 : +if (ressize <sizeof (int16_t )) +return -1 ; +break ; +case INT32 : +case UINT32 : +if (ressize <sizeof (int32_t )) +return -1 ; +break ; +case INT64 : +case UINT64 : +if (ressize <sizeof (int64_t )) +return -1 ; +break ; +default : +return -1 ; + } +return 0 ; +}/* parse an int */ int -cmdline_parse_num (cmdline_parse_token_hdr_t *tk,const char *srcbuf,void *res) +cmdline_parse_num (cmdline_parse_token_hdr_t *tk,const char *srcbuf,void *res, +unsigned ressize) {struct cmdline_token_num_data nd;enum num_parse_state_t st =START ;@@ -141,6 +171,12 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)memcpy (&nd, &((struct cmdline_token_num *)tk)->num_data,sizeof (nd)); +/* check that we have enough room in res */ +if (res) { +if (check_res_size (&nd, ressize) <0 ) +return -1 ; + } +while ( st !=ERROR && c && !cmdline_isendoftoken (c) ) {debug_printf ("%c %x -> " , c, c);switch (st) {lib/librte_cmdline/cmdline_parse_num.h +2
-2@@ -89,9 +89,9 @@ typedef struct cmdline_token_num cmdline_parse_token_num_t;extern struct cmdline_token_ops cmdline_token_num_ops;int cmdline_parse_num (cmdline_parse_token_hdr_t *tk, -const char *srcbuf,void *res); +const char *srcbuf,void *res,unsigned ressize);int cmdline_get_help_num (cmdline_parse_token_hdr_t *tk, -char *dstbuf,unsigned int size); +char *dstbuf,unsigned int size);#define TOKEN_NUM_INITIALIZER (structure, field, numtype) \ { \lib/librte_cmdline/cmdline_parse_portlist.c +4
-1@@ -127,7 +127,7 @@ parse_ports(cmdline_portlist_t * pl, const char * str)int cmdline_parse_portlist (__attribute__((unused))cmdline_parse_token_hdr_t *tk, -const char *buf,void *res) +const char *buf,void *res,unsigned ressize) {unsigned int token_len =0 ;char portlist_str[PORTLIST_TOKEN_SIZE +1 ];@@ -136,6 +136,9 @@ cmdline_parse_portlist(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,if (!buf || ! *buf)return (-1 ); +if (res && ressize <PORTLIST_TOKEN_SIZE ) +return -1 ; + pl = res; while (!cmdline_isendoftoken(buf[token_len]) &&lib/librte_cmdline/cmdline_parse_portlist.h +2
-2@@ -81,9 +81,9 @@ typedef struct cmdline_token_portlist cmdline_parse_token_portlist_t;extern struct cmdline_token_ops cmdline_token_portlist_ops;int cmdline_parse_portlist (cmdline_parse_token_hdr_t *tk, -const char *srcbuf,void *res); +const char *srcbuf,void *res,unsigned ressize);int cmdline_get_help_portlist (cmdline_parse_token_hdr_t *tk, -char *dstbuf,unsigned int size); +char *dstbuf,unsigned int size);#define TOKEN_PORTLIST_INITIALIZER (structure, field) \ { \lib/librte_cmdline/cmdline_parse_string.c +5
-1@@ -105,13 +105,17 @@ get_next_token(const char *s) }int -cmdline_parse_string (cmdline_parse_token_hdr_t *tk,const char *buf,void *res) +cmdline_parse_string (cmdline_parse_token_hdr_t *tk,const char *buf,void *res, +unsigned ressize) {struct cmdline_token_string *tk2;struct cmdline_token_string_data *sd;unsigned int token_len;const char *str; +if (res && ressize <STR_TOKEN_SIZE ) +return -1 ; +if (!tk || !buf || ! *buf)return -1 ;lib/librte_cmdline/cmdline_parse_string.h +1
-1@@ -83,7 +83,7 @@ typedef struct cmdline_token_string cmdline_parse_token_string_t;extern struct cmdline_token_ops cmdline_token_string_ops;int cmdline_parse_string (cmdline_parse_token_hdr_t *tk,const char *srcbuf, -void *res); +void *res,unsigned ressize);int cmdline_complete_get_nb_string (cmdline_parse_token_hdr_t *tk);int cmdline_complete_get_elt_string (cmdline_parse_token_hdr_t *tk,int idx,char *dstbuf,unsigned int size);lib/librte_pmd_bond/rte_eth_bond_args.c +2
-1@@ -254,7 +254,8 @@ bond_ethdev_parse_bond_mac_addr_kvarg(const char *key __rte_unused,return -1 ;/* Parse MAC */ -return cmdline_parse_etheraddr (NULL , value, extra_args); +return cmdline_parse_etheraddr (NULL , value, extra_args, +sizeof (struct ether_addr )); }int Signature · 1 line
2.1.0
Thomas Monjalon
AppliedRe: [PATCH v3] librte_cmdline: FreeBSD Fix oveflow when size of command result structure is greater than BUFSIZ
Quoted 26 lines
Quoted 26 lines
When using test-pmd with flow director in FreeBSD, the application will segfault/Bus error while parsing the command-line. This is due to how each commands result structure is represented during parsing, where the offsets for each tokens value is stored in a character
array(char result_buf[BUFSIZ])in cmdline_parse()(./lib/librte_cmdline/cmdline_parse.c).The overflow occurs where BUFSIZ is less than the size of a commands result structure, in this case "struct
cmd_pkt_filter_result" (app/test-pmd/cmdline.c) is 1088 bytes and BUFSIZ on FreeBSD is 1024 bytes as opposed to 8192 bytes on Linux.The problem can be reproduced by running test-pmd on FreeBSD:
./testpmd -c0x3-n 4 -- -i --portmask=0x3 --pkt-filter-mode=perfect And adding a filter:add_perfect_filter0 udp src 192.168.0.0 1024 dst 192.168.0.0 1024 flexbytes0x800vlan 0 queue 0 soft0x17This patch removes the OS dependency on BUFSIZ and defines and uses a library #define
CMDLINE_PARSE_RESULT_BUFSIZE8192Added boundary checking to ensure this buffer size cannot overflow, with an error message being produced.
Suggested-by Olivier MATZ <[email protected]>http://git.droids-corp.org/?p=libcmdline.git;a=commitdiff;h=b1d5b169352e57df3fc14c51ffad4b83f3e5613f
Quoted 6 lines
Quoted 2 lines
Signed-off-by Alan Carew <[email protected]>Signed-off-by Olivier MATZ <[email protected]>Tested on FreeBSD 10 and this patch fixes the issue described.
Tested-by Bruce Richardson <[email protected]>
Applied
Thank you all
Signature · 1 line
Thomas