1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
14 #include <sys/queue.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <sys/select.h>
19 #include <rte_malloc.h>
20 #include <rte_memory.h>
21 #include <rte_mempool.h>
23 #include <rte_atomic.h>
24 #include <rte_spinlock.h>
26 #include <libvirt/libvirt.h>
28 #include "channel_manager.h"
29 #include "channel_commands.h"
30 #include "channel_monitor.h"
33 #define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1
35 #define ITERATIVE_BITMASK_CHECK_64(mask_u64b, i) \
36 for (i = 0; mask_u64b; mask_u64b &= ~(1ULL << i++)) \
37 if ((mask_u64b >> i) & 1) \
39 /* Global pointer to libvirt connection */
40 static virConnectPtr global_vir_conn_ptr;
42 static unsigned char *global_cpumaps;
43 static virVcpuInfo *global_vircpuinfo;
44 static size_t global_maplen;
46 static unsigned global_n_host_cpus;
49 * Represents a single Virtual Machine
51 struct virtual_machine_info {
52 char name[CHANNEL_MGR_MAX_NAME_LEN];
53 rte_atomic64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS];
54 struct channel_info *channels[CHANNEL_CMDS_MAX_VM_CHANNELS];
55 uint64_t channel_mask;
57 enum vm_status status;
58 virDomainPtr domainPtr;
60 rte_spinlock_t config_spinlock;
61 LIST_ENTRY(virtual_machine_info) vms_info;
64 LIST_HEAD(, virtual_machine_info) vm_list_head;
66 static struct virtual_machine_info *
67 find_domain_by_name(const char *name)
69 struct virtual_machine_info *info;
70 LIST_FOREACH(info, &vm_list_head, vms_info) {
71 if (!strncmp(info->name, name, CHANNEL_MGR_MAX_NAME_LEN-1))
78 update_pcpus_mask(struct virtual_machine_info *vm_info)
80 virVcpuInfoPtr cpuinfo;
85 memset(global_cpumaps, 0, CHANNEL_CMDS_MAX_CPUS*global_maplen);
87 if (!virDomainIsActive(vm_info->domainPtr)) {
88 n_vcpus = virDomainGetVcpuPinInfo(vm_info->domainPtr,
89 vm_info->info.nrVirtCpu, global_cpumaps, global_maplen,
90 VIR_DOMAIN_AFFECT_CONFIG);
92 RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting vCPU info for "
93 "in-active VM '%s'\n", vm_info->name);
99 memset(global_vircpuinfo, 0, sizeof(*global_vircpuinfo)*
100 CHANNEL_CMDS_MAX_CPUS);
102 cpuinfo = global_vircpuinfo;
104 n_vcpus = virDomainGetVcpus(vm_info->domainPtr, cpuinfo,
105 CHANNEL_CMDS_MAX_CPUS, global_cpumaps, global_maplen);
107 RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting vCPU info for "
108 "active VM '%s'\n", vm_info->name);
112 if (n_vcpus >= CHANNEL_CMDS_MAX_CPUS) {
113 RTE_LOG(ERR, CHANNEL_MANAGER, "Number of vCPUS(%u) is out of range "
114 "0...%d\n", n_vcpus, CHANNEL_CMDS_MAX_CPUS-1);
117 if (n_vcpus != vm_info->info.nrVirtCpu) {
118 RTE_LOG(INFO, CHANNEL_MANAGER, "Updating the number of vCPUs for VM '%s"
119 " from %d -> %d\n", vm_info->name, vm_info->info.nrVirtCpu,
121 vm_info->info.nrVirtCpu = n_vcpus;
123 for (i = 0; i < vm_info->info.nrVirtCpu; i++) {
125 for (j = 0; j < global_n_host_cpus; j++) {
126 if (VIR_CPU_USABLE(global_cpumaps, global_maplen, i, j) > 0) {
130 rte_atomic64_set(&vm_info->pcpu_mask[i], mask);
136 set_pcpus_mask(char *vm_name, unsigned vcpu, uint64_t core_mask)
139 int flags = VIR_DOMAIN_AFFECT_LIVE|VIR_DOMAIN_AFFECT_CONFIG;
140 struct virtual_machine_info *vm_info;
141 uint64_t mask = core_mask;
143 if (vcpu >= CHANNEL_CMDS_MAX_CPUS) {
144 RTE_LOG(ERR, CHANNEL_MANAGER, "vCPU(%u) exceeds max allowable(%d)\n",
145 vcpu, CHANNEL_CMDS_MAX_CPUS-1);
149 vm_info = find_domain_by_name(vm_name);
150 if (vm_info == NULL) {
151 RTE_LOG(ERR, CHANNEL_MANAGER, "VM '%s' not found\n", vm_name);
155 if (!virDomainIsActive(vm_info->domainPtr)) {
156 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to set vCPU(%u) to pCPU "
157 "mask(0x%"PRIx64") for VM '%s', VM is not active\n",
158 vcpu, core_mask, vm_info->name);
162 if (vcpu >= vm_info->info.nrVirtCpu) {
163 RTE_LOG(ERR, CHANNEL_MANAGER, "vCPU(%u) exceeds the assigned number of "
164 "vCPUs(%u)\n", vcpu, vm_info->info.nrVirtCpu);
167 memset(global_cpumaps, 0 , CHANNEL_CMDS_MAX_CPUS * global_maplen);
168 ITERATIVE_BITMASK_CHECK_64(mask, i) {
169 VIR_USE_CPU(global_cpumaps, i);
170 if (i >= global_n_host_cpus) {
171 RTE_LOG(ERR, CHANNEL_MANAGER, "CPU(%u) exceeds the available "
172 "number of CPUs(%u)\n", i, global_n_host_cpus);
176 if (virDomainPinVcpuFlags(vm_info->domainPtr, vcpu, global_cpumaps,
177 global_maplen, flags) < 0) {
178 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to set vCPU(%u) to pCPU "
179 "mask(0x%"PRIx64") for VM '%s'\n", vcpu, core_mask,
183 rte_atomic64_set(&vm_info->pcpu_mask[vcpu], core_mask);
189 set_pcpu(char *vm_name, unsigned vcpu, unsigned core_num)
191 uint64_t mask = 1ULL << core_num;
193 return set_pcpus_mask(vm_name, vcpu, mask);
197 get_pcpus_mask(struct channel_info *chan_info, unsigned vcpu)
199 struct virtual_machine_info *vm_info =
200 (struct virtual_machine_info *)chan_info->priv_info;
201 return rte_atomic64_read(&vm_info->pcpu_mask[vcpu]);
205 channel_exists(struct virtual_machine_info *vm_info, unsigned channel_num)
207 rte_spinlock_lock(&(vm_info->config_spinlock));
208 if (vm_info->channel_mask & (1ULL << channel_num)) {
209 rte_spinlock_unlock(&(vm_info->config_spinlock));
212 rte_spinlock_unlock(&(vm_info->config_spinlock));
219 open_non_blocking_channel(struct channel_info *info)
222 struct sockaddr_un sock_addr;
226 info->fd = socket(AF_UNIX, SOCK_STREAM, 0);
227 if (info->fd == -1) {
228 RTE_LOG(ERR, CHANNEL_MANAGER, "Error(%s) creating socket for '%s'\n",
233 sock_addr.sun_family = AF_UNIX;
234 memcpy(&sock_addr.sun_path, info->channel_path,
235 strlen(info->channel_path)+1);
237 /* Get current flags */
238 flags = fcntl(info->fd, F_GETFL, 0);
240 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) fcntl get flags socket for"
241 "'%s'\n", strerror(errno), info->channel_path);
244 /* Set to Non Blocking */
246 if (fcntl(info->fd, F_SETFL, flags) < 0) {
247 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) setting non-blocking "
248 "socket for '%s'\n", strerror(errno), info->channel_path);
251 ret = connect(info->fd, (struct sockaddr *)&sock_addr,
254 /* ECONNREFUSED error is given when VM is not active */
255 if (errno == ECONNREFUSED) {
256 RTE_LOG(WARNING, CHANNEL_MANAGER, "VM is not active or has not "
257 "activated its endpoint to channel %s\n",
261 /* Wait for tv_sec if in progress */
262 else if (errno == EINPROGRESS) {
265 FD_ZERO(&soc_fd_set);
266 FD_SET(info->fd, &soc_fd_set);
267 if (select(info->fd+1, NULL, &soc_fd_set, NULL, &tv) > 0) {
268 RTE_LOG(WARNING, CHANNEL_MANAGER, "Timeout or error on channel "
269 "'%s'\n", info->channel_path);
273 /* Any other error */
274 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) connecting socket"
275 " for '%s'\n", strerror(errno), info->channel_path);
283 setup_channel_info(struct virtual_machine_info **vm_info_dptr,
284 struct channel_info **chan_info_dptr, unsigned channel_num)
286 struct channel_info *chan_info = *chan_info_dptr;
287 struct virtual_machine_info *vm_info = *vm_info_dptr;
289 chan_info->channel_num = channel_num;
290 chan_info->priv_info = (void *)vm_info;
291 chan_info->status = CHANNEL_MGR_CHANNEL_DISCONNECTED;
292 if (open_non_blocking_channel(chan_info) < 0) {
293 RTE_LOG(ERR, CHANNEL_MANAGER, "Could not open channel: "
294 "'%s' for VM '%s'\n",
295 chan_info->channel_path, vm_info->name);
298 if (add_channel_to_monitor(&chan_info) < 0) {
299 RTE_LOG(ERR, CHANNEL_MANAGER, "Could add channel: "
300 "'%s' to epoll ctl for VM '%s'\n",
301 chan_info->channel_path, vm_info->name);
305 rte_spinlock_lock(&(vm_info->config_spinlock));
306 vm_info->num_channels++;
307 vm_info->channel_mask |= 1ULL << channel_num;
308 vm_info->channels[channel_num] = chan_info;
309 chan_info->status = CHANNEL_MGR_CHANNEL_CONNECTED;
310 rte_spinlock_unlock(&(vm_info->config_spinlock));
315 add_all_channels(const char *vm_name)
319 struct virtual_machine_info *vm_info;
320 struct channel_info *chan_info;
321 char *token, *remaining, *tail_ptr;
322 char socket_name[PATH_MAX];
323 unsigned channel_num;
324 int num_channels_enabled = 0;
326 /* verify VM exists */
327 vm_info = find_domain_by_name(vm_name);
328 if (vm_info == NULL) {
329 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' not found"
330 " during channel discovery\n", vm_name);
333 if (!virDomainIsActive(vm_info->domainPtr)) {
334 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' is not active\n", vm_name);
335 vm_info->status = CHANNEL_MGR_VM_INACTIVE;
338 d = opendir(CHANNEL_MGR_SOCKET_PATH);
340 RTE_LOG(ERR, CHANNEL_MANAGER, "Error opening directory '%s': %s\n",
341 CHANNEL_MGR_SOCKET_PATH, strerror(errno));
344 while ((dir = readdir(d)) != NULL) {
345 if (!strncmp(dir->d_name, ".", 1) ||
346 !strncmp(dir->d_name, "..", 2))
349 snprintf(socket_name, sizeof(socket_name), "%s", dir->d_name);
350 remaining = socket_name;
351 /* Extract vm_name from "<vm_name>.<channel_num>" */
352 token = strsep(&remaining, ".");
353 if (remaining == NULL)
355 if (strncmp(vm_name, token, CHANNEL_MGR_MAX_NAME_LEN))
358 /* remaining should contain only <channel_num> */
360 channel_num = (unsigned)strtol(remaining, &tail_ptr, 0);
361 if ((errno != 0) || (remaining[0] == '\0') ||
362 tail_ptr == NULL || (*tail_ptr != '\0')) {
363 RTE_LOG(WARNING, CHANNEL_MANAGER, "Malformed channel name"
364 "'%s' found it should be in the form of "
365 "'<guest_name>.<channel_num>(decimal)'\n",
369 if (channel_num >= CHANNEL_CMDS_MAX_VM_CHANNELS) {
370 RTE_LOG(WARNING, CHANNEL_MANAGER, "Channel number(%u) is "
371 "greater than max allowable: %d, skipping '%s%s'\n",
372 channel_num, CHANNEL_CMDS_MAX_VM_CHANNELS-1,
373 CHANNEL_MGR_SOCKET_PATH, dir->d_name);
376 /* if channel has not been added previously */
377 if (channel_exists(vm_info, channel_num))
380 chan_info = rte_malloc(NULL, sizeof(*chan_info),
381 RTE_CACHE_LINE_SIZE);
382 if (chan_info == NULL) {
383 RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for "
384 "channel '%s%s'\n", CHANNEL_MGR_SOCKET_PATH, dir->d_name);
388 snprintf(chan_info->channel_path,
389 sizeof(chan_info->channel_path), "%s%s",
390 CHANNEL_MGR_SOCKET_PATH, dir->d_name);
392 if (setup_channel_info(&vm_info, &chan_info, channel_num) < 0) {
397 num_channels_enabled++;
400 return num_channels_enabled;
404 add_channels(const char *vm_name, unsigned *channel_list,
405 unsigned len_channel_list)
407 struct virtual_machine_info *vm_info;
408 struct channel_info *chan_info;
409 char socket_path[PATH_MAX];
411 int num_channels_enabled = 0;
413 vm_info = find_domain_by_name(vm_name);
414 if (vm_info == NULL) {
415 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add channels: VM '%s' "
416 "not found\n", vm_name);
420 if (!virDomainIsActive(vm_info->domainPtr)) {
421 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' is not active\n", vm_name);
422 vm_info->status = CHANNEL_MGR_VM_INACTIVE;
426 for (i = 0; i < len_channel_list; i++) {
428 if (channel_list[i] >= CHANNEL_CMDS_MAX_VM_CHANNELS) {
429 RTE_LOG(INFO, CHANNEL_MANAGER, "Channel(%u) is out of range "
430 "0...%d\n", channel_list[i],
431 CHANNEL_CMDS_MAX_VM_CHANNELS-1);
434 if (channel_exists(vm_info, channel_list[i])) {
435 RTE_LOG(INFO, CHANNEL_MANAGER, "Channel already exists, skipping "
436 "'%s.%u'\n", vm_name, i);
440 snprintf(socket_path, sizeof(socket_path), "%s%s.%u",
441 CHANNEL_MGR_SOCKET_PATH, vm_name, channel_list[i]);
443 if (access(socket_path, F_OK) < 0) {
444 RTE_LOG(ERR, CHANNEL_MANAGER, "Channel path '%s' error: "
445 "%s\n", socket_path, strerror(errno));
448 chan_info = rte_malloc(NULL, sizeof(*chan_info),
449 RTE_CACHE_LINE_SIZE);
450 if (chan_info == NULL) {
451 RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for "
452 "channel '%s'\n", socket_path);
455 snprintf(chan_info->channel_path,
456 sizeof(chan_info->channel_path), "%s%s.%u",
457 CHANNEL_MGR_SOCKET_PATH, vm_name, channel_list[i]);
458 if (setup_channel_info(&vm_info, &chan_info, channel_list[i]) < 0) {
462 num_channels_enabled++;
465 return num_channels_enabled;
469 remove_channel(struct channel_info **chan_info_dptr)
471 struct virtual_machine_info *vm_info;
472 struct channel_info *chan_info = *chan_info_dptr;
474 close(chan_info->fd);
476 vm_info = (struct virtual_machine_info *)chan_info->priv_info;
478 rte_spinlock_lock(&(vm_info->config_spinlock));
479 vm_info->channel_mask &= ~(1ULL << chan_info->channel_num);
480 vm_info->num_channels--;
481 rte_spinlock_unlock(&(vm_info->config_spinlock));
488 set_channel_status_all(const char *vm_name, enum channel_status status)
490 struct virtual_machine_info *vm_info;
493 int num_channels_changed = 0;
495 if (!(status == CHANNEL_MGR_CHANNEL_CONNECTED ||
496 status == CHANNEL_MGR_CHANNEL_DISABLED)) {
497 RTE_LOG(ERR, CHANNEL_MANAGER, "Channels can only be enabled or "
498 "disabled: Unable to change status for VM '%s'\n", vm_name);
500 vm_info = find_domain_by_name(vm_name);
501 if (vm_info == NULL) {
502 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to disable channels: VM '%s' "
503 "not found\n", vm_name);
507 rte_spinlock_lock(&(vm_info->config_spinlock));
508 mask = vm_info->channel_mask;
509 ITERATIVE_BITMASK_CHECK_64(mask, i) {
510 vm_info->channels[i]->status = status;
511 num_channels_changed++;
513 rte_spinlock_unlock(&(vm_info->config_spinlock));
514 return num_channels_changed;
519 set_channel_status(const char *vm_name, unsigned *channel_list,
520 unsigned len_channel_list, enum channel_status status)
522 struct virtual_machine_info *vm_info;
524 int num_channels_changed = 0;
526 if (!(status == CHANNEL_MGR_CHANNEL_CONNECTED ||
527 status == CHANNEL_MGR_CHANNEL_DISABLED)) {
528 RTE_LOG(ERR, CHANNEL_MANAGER, "Channels can only be enabled or "
529 "disabled: Unable to change status for VM '%s'\n", vm_name);
531 vm_info = find_domain_by_name(vm_name);
532 if (vm_info == NULL) {
533 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add channels: VM '%s' "
534 "not found\n", vm_name);
537 for (i = 0; i < len_channel_list; i++) {
538 if (channel_exists(vm_info, channel_list[i])) {
539 rte_spinlock_lock(&(vm_info->config_spinlock));
540 vm_info->channels[channel_list[i]]->status = status;
541 rte_spinlock_unlock(&(vm_info->config_spinlock));
542 num_channels_changed++;
545 return num_channels_changed;
549 get_all_vm(int *num_vm, int *num_vcpu)
552 virNodeInfo node_info;
553 virDomainPtr *domptr;
555 int i, ii, numVcpus[MAX_VCPUS], cpu, n_vcpus;
558 unsigned int domain_flags = VIR_CONNECT_LIST_DOMAINS_RUNNING |
559 VIR_CONNECT_LIST_DOMAINS_PERSISTENT;
560 unsigned int domain_flag = VIR_DOMAIN_VCPU_CONFIG;
563 memset(global_cpumaps, 0, CHANNEL_CMDS_MAX_CPUS*global_maplen);
564 if (virNodeGetInfo(global_vir_conn_ptr, &node_info)) {
565 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to retrieve node Info\n");
569 /* Returns number of pcpus */
570 global_n_host_cpus = (unsigned int)node_info.cpus;
572 /* Returns number of active domains */
573 *num_vm = virConnectListAllDomains(global_vir_conn_ptr, &domptr,
576 RTE_LOG(ERR, CHANNEL_MANAGER, "No Active Domains Running\n");
580 for (i = 0; i < *num_vm; i++) {
582 /* Get Domain Names */
583 vm_name = virDomainGetName(domptr[i]);
584 lvm_info[i].vm_name = vm_name;
586 /* Get Number of Vcpus */
587 numVcpus[i] = virDomainGetVcpusFlags(domptr[i], domain_flag);
589 /* Get Number of VCpus & VcpuPinInfo */
590 n_vcpus = virDomainGetVcpuPinInfo(domptr[i],
591 numVcpus[i], global_cpumaps,
592 global_maplen, domain_flag);
594 if ((int)n_vcpus > 0) {
596 lvm_info[i].num_cpus = n_vcpus;
599 /* Save pcpu in use by libvirt VMs */
600 for (ii = 0; ii < n_vcpus; ii++) {
602 for (jj = 0; jj < global_n_host_cpus; jj++) {
603 if (VIR_CPU_USABLE(global_cpumaps,
604 global_maplen, ii, jj) > 0) {
608 ITERATIVE_BITMASK_CHECK_64(mask, cpu) {
609 lvm_info[i].pcpus[ii] = cpu;
616 get_info_vm(const char *vm_name, struct vm_info *info)
618 struct virtual_machine_info *vm_info;
619 unsigned i, channel_num = 0;
622 vm_info = find_domain_by_name(vm_name);
623 if (vm_info == NULL) {
624 RTE_LOG(ERR, CHANNEL_MANAGER, "VM '%s' not found\n", vm_name);
627 info->status = CHANNEL_MGR_VM_ACTIVE;
628 if (!virDomainIsActive(vm_info->domainPtr))
629 info->status = CHANNEL_MGR_VM_INACTIVE;
631 rte_spinlock_lock(&(vm_info->config_spinlock));
633 mask = vm_info->channel_mask;
634 ITERATIVE_BITMASK_CHECK_64(mask, i) {
635 info->channels[channel_num].channel_num = i;
636 memcpy(info->channels[channel_num].channel_path,
637 vm_info->channels[i]->channel_path, UNIX_PATH_MAX);
638 info->channels[channel_num].status = vm_info->channels[i]->status;
639 info->channels[channel_num].fd = vm_info->channels[i]->fd;
643 info->num_channels = channel_num;
644 info->num_vcpus = vm_info->info.nrVirtCpu;
645 rte_spinlock_unlock(&(vm_info->config_spinlock));
647 memcpy(info->name, vm_info->name, sizeof(vm_info->name));
648 for (i = 0; i < info->num_vcpus; i++) {
649 info->pcpu_mask[i] = rte_atomic64_read(&vm_info->pcpu_mask[i]);
655 add_vm(const char *vm_name)
657 struct virtual_machine_info *new_domain;
658 virDomainPtr dom_ptr;
661 if (find_domain_by_name(vm_name) != NULL) {
662 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add VM: VM '%s' "
663 "already exists\n", vm_name);
667 if (global_vir_conn_ptr == NULL) {
668 RTE_LOG(ERR, CHANNEL_MANAGER, "No connection to hypervisor exists\n");
671 dom_ptr = virDomainLookupByName(global_vir_conn_ptr, vm_name);
672 if (dom_ptr == NULL) {
673 RTE_LOG(ERR, CHANNEL_MANAGER, "Error on VM lookup with libvirt: "
674 "VM '%s' not found\n", vm_name);
678 new_domain = rte_malloc("virtual_machine_info", sizeof(*new_domain),
679 RTE_CACHE_LINE_SIZE);
680 if (new_domain == NULL) {
681 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to allocate memory for VM "
685 new_domain->domainPtr = dom_ptr;
686 if (virDomainGetInfo(new_domain->domainPtr, &new_domain->info) != 0) {
687 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to get libvirt VM info\n");
688 rte_free(new_domain);
691 if (new_domain->info.nrVirtCpu > CHANNEL_CMDS_MAX_CPUS) {
692 RTE_LOG(ERR, CHANNEL_MANAGER, "Error the number of virtual CPUs(%u) is "
693 "greater than allowable(%d)\n", new_domain->info.nrVirtCpu,
694 CHANNEL_CMDS_MAX_CPUS);
695 rte_free(new_domain);
699 for (i = 0; i < CHANNEL_CMDS_MAX_CPUS; i++) {
700 rte_atomic64_init(&new_domain->pcpu_mask[i]);
702 if (update_pcpus_mask(new_domain) < 0) {
703 RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting physical CPU pinning\n");
704 rte_free(new_domain);
707 strncpy(new_domain->name, vm_name, sizeof(new_domain->name));
708 new_domain->name[sizeof(new_domain->name) - 1] = '\0';
709 new_domain->channel_mask = 0;
710 new_domain->num_channels = 0;
712 if (!virDomainIsActive(dom_ptr))
713 new_domain->status = CHANNEL_MGR_VM_INACTIVE;
715 new_domain->status = CHANNEL_MGR_VM_ACTIVE;
717 rte_spinlock_init(&(new_domain->config_spinlock));
718 LIST_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
723 remove_vm(const char *vm_name)
725 struct virtual_machine_info *vm_info = find_domain_by_name(vm_name);
727 if (vm_info == NULL) {
728 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to remove VM: VM '%s' "
729 "not found\n", vm_name);
732 rte_spinlock_lock(&vm_info->config_spinlock);
733 if (vm_info->num_channels != 0) {
734 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to remove VM '%s', there are "
735 "%"PRId8" channels still active\n",
736 vm_name, vm_info->num_channels);
737 rte_spinlock_unlock(&vm_info->config_spinlock);
740 LIST_REMOVE(vm_info, vms_info);
741 rte_spinlock_unlock(&vm_info->config_spinlock);
747 disconnect_hypervisor(void)
749 if (global_vir_conn_ptr != NULL) {
750 virConnectClose(global_vir_conn_ptr);
751 global_vir_conn_ptr = NULL;
756 connect_hypervisor(const char *path)
758 if (global_vir_conn_ptr != NULL) {
759 RTE_LOG(ERR, CHANNEL_MANAGER, "Error connecting to %s, connection "
760 "already established\n", path);
763 global_vir_conn_ptr = virConnectOpen(path);
764 if (global_vir_conn_ptr == NULL) {
765 RTE_LOG(ERR, CHANNEL_MANAGER, "Error failed to open connection to "
766 "Hypervisor '%s'\n", path);
773 channel_manager_init(const char *path)
777 LIST_INIT(&vm_list_head);
778 if (connect_hypervisor(path) < 0) {
779 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to initialize channel manager\n");
783 global_maplen = VIR_CPU_MAPLEN(CHANNEL_CMDS_MAX_CPUS);
785 global_vircpuinfo = rte_zmalloc(NULL, sizeof(*global_vircpuinfo) *
786 CHANNEL_CMDS_MAX_CPUS, RTE_CACHE_LINE_SIZE);
787 if (global_vircpuinfo == NULL) {
788 RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for CPU Info\n");
791 global_cpumaps = rte_zmalloc(NULL, CHANNEL_CMDS_MAX_CPUS * global_maplen,
792 RTE_CACHE_LINE_SIZE);
793 if (global_cpumaps == NULL) {
797 if (virNodeGetInfo(global_vir_conn_ptr, &info)) {
798 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to retrieve node Info\n");
802 global_n_host_cpus = (unsigned)info.cpus;
804 if (global_n_host_cpus > CHANNEL_CMDS_MAX_CPUS) {
805 RTE_LOG(WARNING, CHANNEL_MANAGER, "The number of host CPUs(%u) exceeds the "
806 "maximum of %u. No cores over %u should be used.\n",
807 global_n_host_cpus, CHANNEL_CMDS_MAX_CPUS,
808 CHANNEL_CMDS_MAX_CPUS - 1);
809 global_n_host_cpus = CHANNEL_CMDS_MAX_CPUS;
814 disconnect_hypervisor();
819 channel_manager_exit(void)
823 struct virtual_machine_info *vm_info;
825 LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
827 rte_spinlock_lock(&(vm_info->config_spinlock));
829 mask = vm_info->channel_mask;
830 ITERATIVE_BITMASK_CHECK_64(mask, i) {
831 remove_channel_from_monitor(vm_info->channels[i]);
832 close(vm_info->channels[i]->fd);
833 rte_free(vm_info->channels[i]);
835 rte_spinlock_unlock(&(vm_info->config_spinlock));
837 LIST_REMOVE(vm_info, vms_info);
841 rte_free(global_cpumaps);
842 rte_free(global_vircpuinfo);
843 disconnect_hypervisor();