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 int global_n_host_cpus;
47 static bool global_hypervisor_available;
50 * Represents a single Virtual Machine
52 struct virtual_machine_info {
53 char name[CHANNEL_MGR_MAX_NAME_LEN];
54 rte_atomic64_t pcpu_mask[CHANNEL_CMDS_MAX_CPUS];
55 struct channel_info *channels[CHANNEL_CMDS_MAX_VM_CHANNELS];
56 uint64_t channel_mask;
58 enum vm_status status;
59 virDomainPtr domainPtr;
61 rte_spinlock_t config_spinlock;
62 LIST_ENTRY(virtual_machine_info) vms_info;
65 LIST_HEAD(, virtual_machine_info) vm_list_head;
67 static struct virtual_machine_info *
68 find_domain_by_name(const char *name)
70 struct virtual_machine_info *info;
71 LIST_FOREACH(info, &vm_list_head, vms_info) {
72 if (!strncmp(info->name, name, CHANNEL_MGR_MAX_NAME_LEN-1))
79 update_pcpus_mask(struct virtual_machine_info *vm_info)
81 virVcpuInfoPtr cpuinfo;
86 memset(global_cpumaps, 0, CHANNEL_CMDS_MAX_CPUS*global_maplen);
88 if (!virDomainIsActive(vm_info->domainPtr)) {
89 n_vcpus = virDomainGetVcpuPinInfo(vm_info->domainPtr,
90 vm_info->info.nrVirtCpu, global_cpumaps, global_maplen,
91 VIR_DOMAIN_AFFECT_CONFIG);
93 RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting vCPU info for "
94 "in-active VM '%s'\n", vm_info->name);
100 memset(global_vircpuinfo, 0, sizeof(*global_vircpuinfo)*
101 CHANNEL_CMDS_MAX_CPUS);
103 cpuinfo = global_vircpuinfo;
105 n_vcpus = virDomainGetVcpus(vm_info->domainPtr, cpuinfo,
106 CHANNEL_CMDS_MAX_CPUS, global_cpumaps, global_maplen);
108 RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting vCPU info for "
109 "active VM '%s'\n", vm_info->name);
113 if (n_vcpus >= CHANNEL_CMDS_MAX_CPUS) {
114 RTE_LOG(ERR, CHANNEL_MANAGER, "Number of vCPUS(%u) is out of range "
115 "0...%d\n", n_vcpus, CHANNEL_CMDS_MAX_CPUS-1);
118 if (n_vcpus != vm_info->info.nrVirtCpu) {
119 RTE_LOG(INFO, CHANNEL_MANAGER, "Updating the number of vCPUs for VM '%s"
120 " from %d -> %d\n", vm_info->name, vm_info->info.nrVirtCpu,
122 vm_info->info.nrVirtCpu = n_vcpus;
124 for (i = 0; i < vm_info->info.nrVirtCpu; i++) {
126 for (j = 0; j < global_n_host_cpus; j++) {
127 if (VIR_CPU_USABLE(global_cpumaps, global_maplen, i, j) > 0) {
131 rte_atomic64_set(&vm_info->pcpu_mask[i], mask);
137 set_pcpus_mask(char *vm_name, unsigned vcpu, uint64_t core_mask)
140 int flags = VIR_DOMAIN_AFFECT_LIVE|VIR_DOMAIN_AFFECT_CONFIG;
141 struct virtual_machine_info *vm_info;
142 uint64_t mask = core_mask;
144 if (vcpu >= CHANNEL_CMDS_MAX_CPUS) {
145 RTE_LOG(ERR, CHANNEL_MANAGER, "vCPU(%u) exceeds max allowable(%d)\n",
146 vcpu, CHANNEL_CMDS_MAX_CPUS-1);
150 vm_info = find_domain_by_name(vm_name);
151 if (vm_info == NULL) {
152 RTE_LOG(ERR, CHANNEL_MANAGER, "VM '%s' not found\n", vm_name);
156 if (!virDomainIsActive(vm_info->domainPtr)) {
157 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to set vCPU(%u) to pCPU "
158 "mask(0x%"PRIx64") for VM '%s', VM is not active\n",
159 vcpu, core_mask, vm_info->name);
163 if (vcpu >= vm_info->info.nrVirtCpu) {
164 RTE_LOG(ERR, CHANNEL_MANAGER, "vCPU(%u) exceeds the assigned number of "
165 "vCPUs(%u)\n", vcpu, vm_info->info.nrVirtCpu);
168 memset(global_cpumaps, 0 , CHANNEL_CMDS_MAX_CPUS * global_maplen);
169 ITERATIVE_BITMASK_CHECK_64(mask, i) {
170 VIR_USE_CPU(global_cpumaps, i);
171 if (i >= global_n_host_cpus) {
172 RTE_LOG(ERR, CHANNEL_MANAGER, "CPU(%u) exceeds the available "
173 "number of CPUs(%u)\n", i, global_n_host_cpus);
177 if (virDomainPinVcpuFlags(vm_info->domainPtr, vcpu, global_cpumaps,
178 global_maplen, flags) < 0) {
179 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to set vCPU(%u) to pCPU "
180 "mask(0x%"PRIx64") for VM '%s'\n", vcpu, core_mask,
184 rte_atomic64_set(&vm_info->pcpu_mask[vcpu], core_mask);
190 set_pcpu(char *vm_name, unsigned vcpu, unsigned core_num)
192 uint64_t mask = 1ULL << core_num;
194 return set_pcpus_mask(vm_name, vcpu, mask);
198 get_pcpus_mask(struct channel_info *chan_info, unsigned vcpu)
200 struct virtual_machine_info *vm_info =
201 (struct virtual_machine_info *)chan_info->priv_info;
203 if (global_hypervisor_available && (vm_info != NULL))
204 return rte_atomic64_read(&vm_info->pcpu_mask[vcpu]);
210 channel_exists(struct virtual_machine_info *vm_info, unsigned channel_num)
212 rte_spinlock_lock(&(vm_info->config_spinlock));
213 if (vm_info->channel_mask & (1ULL << channel_num)) {
214 rte_spinlock_unlock(&(vm_info->config_spinlock));
217 rte_spinlock_unlock(&(vm_info->config_spinlock));
224 open_non_blocking_channel(struct channel_info *info)
227 struct sockaddr_un sock_addr;
231 info->fd = socket(AF_UNIX, SOCK_STREAM, 0);
232 if (info->fd == -1) {
233 RTE_LOG(ERR, CHANNEL_MANAGER, "Error(%s) creating socket for '%s'\n",
238 sock_addr.sun_family = AF_UNIX;
239 memcpy(&sock_addr.sun_path, info->channel_path,
240 strlen(info->channel_path)+1);
242 /* Get current flags */
243 flags = fcntl(info->fd, F_GETFL, 0);
245 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) fcntl get flags socket for"
246 "'%s'\n", strerror(errno), info->channel_path);
249 /* Set to Non Blocking */
251 if (fcntl(info->fd, F_SETFL, flags) < 0) {
252 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) setting non-blocking "
253 "socket for '%s'\n", strerror(errno), info->channel_path);
256 ret = connect(info->fd, (struct sockaddr *)&sock_addr,
259 /* ECONNREFUSED error is given when VM is not active */
260 if (errno == ECONNREFUSED) {
261 RTE_LOG(WARNING, CHANNEL_MANAGER, "VM is not active or has not "
262 "activated its endpoint to channel %s\n",
266 /* Wait for tv_sec if in progress */
267 else if (errno == EINPROGRESS) {
270 FD_ZERO(&soc_fd_set);
271 FD_SET(info->fd, &soc_fd_set);
272 if (select(info->fd+1, NULL, &soc_fd_set, NULL, &tv) > 0) {
273 RTE_LOG(WARNING, CHANNEL_MANAGER, "Timeout or error on channel "
274 "'%s'\n", info->channel_path);
278 /* Any other error */
279 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) connecting socket"
280 " for '%s'\n", strerror(errno), info->channel_path);
288 setup_channel_info(struct virtual_machine_info **vm_info_dptr,
289 struct channel_info **chan_info_dptr, unsigned channel_num)
291 struct channel_info *chan_info = *chan_info_dptr;
292 struct virtual_machine_info *vm_info = *vm_info_dptr;
294 chan_info->channel_num = channel_num;
295 chan_info->priv_info = (void *)vm_info;
296 chan_info->status = CHANNEL_MGR_CHANNEL_DISCONNECTED;
297 if (open_non_blocking_channel(chan_info) < 0) {
298 RTE_LOG(ERR, CHANNEL_MANAGER, "Could not open channel: "
299 "'%s' for VM '%s'\n",
300 chan_info->channel_path, vm_info->name);
303 if (add_channel_to_monitor(&chan_info) < 0) {
304 RTE_LOG(ERR, CHANNEL_MANAGER, "Could add channel: "
305 "'%s' to epoll ctl for VM '%s'\n",
306 chan_info->channel_path, vm_info->name);
310 rte_spinlock_lock(&(vm_info->config_spinlock));
311 vm_info->num_channels++;
312 vm_info->channel_mask |= 1ULL << channel_num;
313 vm_info->channels[channel_num] = chan_info;
314 chan_info->status = CHANNEL_MGR_CHANNEL_CONNECTED;
315 rte_spinlock_unlock(&(vm_info->config_spinlock));
320 add_all_channels(const char *vm_name)
324 struct virtual_machine_info *vm_info;
325 struct channel_info *chan_info;
326 char *token, *remaining, *tail_ptr;
327 char socket_name[PATH_MAX];
328 unsigned channel_num;
329 int num_channels_enabled = 0;
331 /* verify VM exists */
332 vm_info = find_domain_by_name(vm_name);
333 if (vm_info == NULL) {
334 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' not found"
335 " during channel discovery\n", vm_name);
338 if (!virDomainIsActive(vm_info->domainPtr)) {
339 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' is not active\n", vm_name);
340 vm_info->status = CHANNEL_MGR_VM_INACTIVE;
343 d = opendir(CHANNEL_MGR_SOCKET_PATH);
345 RTE_LOG(ERR, CHANNEL_MANAGER, "Error opening directory '%s': %s\n",
346 CHANNEL_MGR_SOCKET_PATH, strerror(errno));
349 while ((dir = readdir(d)) != NULL) {
350 if (!strncmp(dir->d_name, ".", 1) ||
351 !strncmp(dir->d_name, "..", 2))
354 snprintf(socket_name, sizeof(socket_name), "%s", dir->d_name);
355 remaining = socket_name;
356 /* Extract vm_name from "<vm_name>.<channel_num>" */
357 token = strsep(&remaining, ".");
358 if (remaining == NULL)
360 if (strncmp(vm_name, token, CHANNEL_MGR_MAX_NAME_LEN))
363 /* remaining should contain only <channel_num> */
365 channel_num = (unsigned)strtol(remaining, &tail_ptr, 0);
366 if ((errno != 0) || (remaining[0] == '\0') ||
367 tail_ptr == NULL || (*tail_ptr != '\0')) {
368 RTE_LOG(WARNING, CHANNEL_MANAGER, "Malformed channel name"
369 "'%s' found it should be in the form of "
370 "'<guest_name>.<channel_num>(decimal)'\n",
374 if (channel_num >= CHANNEL_CMDS_MAX_VM_CHANNELS) {
375 RTE_LOG(WARNING, CHANNEL_MANAGER, "Channel number(%u) is "
376 "greater than max allowable: %d, skipping '%s%s'\n",
377 channel_num, CHANNEL_CMDS_MAX_VM_CHANNELS-1,
378 CHANNEL_MGR_SOCKET_PATH, dir->d_name);
381 /* if channel has not been added previously */
382 if (channel_exists(vm_info, channel_num))
385 chan_info = rte_malloc(NULL, sizeof(*chan_info),
386 RTE_CACHE_LINE_SIZE);
387 if (chan_info == NULL) {
388 RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for "
389 "channel '%s%s'\n", CHANNEL_MGR_SOCKET_PATH, dir->d_name);
393 snprintf(chan_info->channel_path,
394 sizeof(chan_info->channel_path), "%s%s",
395 CHANNEL_MGR_SOCKET_PATH, dir->d_name);
397 if (setup_channel_info(&vm_info, &chan_info, channel_num) < 0) {
402 num_channels_enabled++;
405 return num_channels_enabled;
409 add_channels(const char *vm_name, unsigned *channel_list,
410 unsigned len_channel_list)
412 struct virtual_machine_info *vm_info;
413 struct channel_info *chan_info;
414 char socket_path[PATH_MAX];
416 int num_channels_enabled = 0;
418 vm_info = find_domain_by_name(vm_name);
419 if (vm_info == NULL) {
420 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add channels: VM '%s' "
421 "not found\n", vm_name);
425 if (!virDomainIsActive(vm_info->domainPtr)) {
426 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' is not active\n", vm_name);
427 vm_info->status = CHANNEL_MGR_VM_INACTIVE;
431 for (i = 0; i < len_channel_list; i++) {
433 if (channel_list[i] >= CHANNEL_CMDS_MAX_VM_CHANNELS) {
434 RTE_LOG(INFO, CHANNEL_MANAGER, "Channel(%u) is out of range "
435 "0...%d\n", channel_list[i],
436 CHANNEL_CMDS_MAX_VM_CHANNELS-1);
439 if (channel_exists(vm_info, channel_list[i])) {
440 RTE_LOG(INFO, CHANNEL_MANAGER, "Channel already exists, skipping "
441 "'%s.%u'\n", vm_name, i);
445 snprintf(socket_path, sizeof(socket_path), "%s%s.%u",
446 CHANNEL_MGR_SOCKET_PATH, vm_name, channel_list[i]);
448 if (access(socket_path, F_OK) < 0) {
449 RTE_LOG(ERR, CHANNEL_MANAGER, "Channel path '%s' error: "
450 "%s\n", socket_path, strerror(errno));
453 chan_info = rte_malloc(NULL, sizeof(*chan_info),
454 RTE_CACHE_LINE_SIZE);
455 if (chan_info == NULL) {
456 RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for "
457 "channel '%s'\n", socket_path);
460 snprintf(chan_info->channel_path,
461 sizeof(chan_info->channel_path), "%s%s.%u",
462 CHANNEL_MGR_SOCKET_PATH, vm_name, channel_list[i]);
463 if (setup_channel_info(&vm_info, &chan_info, channel_list[i]) < 0) {
467 num_channels_enabled++;
470 return num_channels_enabled;
474 remove_channel(struct channel_info **chan_info_dptr)
476 struct virtual_machine_info *vm_info;
477 struct channel_info *chan_info = *chan_info_dptr;
479 close(chan_info->fd);
481 vm_info = (struct virtual_machine_info *)chan_info->priv_info;
483 rte_spinlock_lock(&(vm_info->config_spinlock));
484 vm_info->channel_mask &= ~(1ULL << chan_info->channel_num);
485 vm_info->num_channels--;
486 rte_spinlock_unlock(&(vm_info->config_spinlock));
493 set_channel_status_all(const char *vm_name, enum channel_status status)
495 struct virtual_machine_info *vm_info;
498 int num_channels_changed = 0;
500 if (!(status == CHANNEL_MGR_CHANNEL_CONNECTED ||
501 status == CHANNEL_MGR_CHANNEL_DISABLED)) {
502 RTE_LOG(ERR, CHANNEL_MANAGER, "Channels can only be enabled or "
503 "disabled: Unable to change status for VM '%s'\n", vm_name);
505 vm_info = find_domain_by_name(vm_name);
506 if (vm_info == NULL) {
507 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to disable channels: VM '%s' "
508 "not found\n", vm_name);
512 rte_spinlock_lock(&(vm_info->config_spinlock));
513 mask = vm_info->channel_mask;
514 ITERATIVE_BITMASK_CHECK_64(mask, i) {
515 vm_info->channels[i]->status = status;
516 num_channels_changed++;
518 rte_spinlock_unlock(&(vm_info->config_spinlock));
519 return num_channels_changed;
524 set_channel_status(const char *vm_name, unsigned *channel_list,
525 unsigned len_channel_list, enum channel_status status)
527 struct virtual_machine_info *vm_info;
529 int num_channels_changed = 0;
531 if (!(status == CHANNEL_MGR_CHANNEL_CONNECTED ||
532 status == CHANNEL_MGR_CHANNEL_DISABLED)) {
533 RTE_LOG(ERR, CHANNEL_MANAGER, "Channels can only be enabled or "
534 "disabled: Unable to change status for VM '%s'\n", vm_name);
536 vm_info = find_domain_by_name(vm_name);
537 if (vm_info == NULL) {
538 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add channels: VM '%s' "
539 "not found\n", vm_name);
542 for (i = 0; i < len_channel_list; i++) {
543 if (channel_exists(vm_info, channel_list[i])) {
544 rte_spinlock_lock(&(vm_info->config_spinlock));
545 vm_info->channels[channel_list[i]]->status = status;
546 rte_spinlock_unlock(&(vm_info->config_spinlock));
547 num_channels_changed++;
550 return num_channels_changed;
554 get_all_vm(int *num_vm, int *num_vcpu)
557 virNodeInfo node_info;
558 virDomainPtr *domptr;
560 int i, ii, numVcpus[MAX_VCPUS], cpu, n_vcpus;
563 unsigned int domain_flags = VIR_CONNECT_LIST_DOMAINS_RUNNING |
564 VIR_CONNECT_LIST_DOMAINS_PERSISTENT;
565 unsigned int domain_flag = VIR_DOMAIN_VCPU_CONFIG;
567 if (!global_hypervisor_available)
570 memset(global_cpumaps, 0, CHANNEL_CMDS_MAX_CPUS*global_maplen);
571 if (virNodeGetInfo(global_vir_conn_ptr, &node_info)) {
572 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to retrieve node Info\n");
576 /* Returns number of pcpus */
577 global_n_host_cpus = (unsigned int)node_info.cpus;
579 /* Returns number of active domains */
580 *num_vm = virConnectListAllDomains(global_vir_conn_ptr, &domptr,
583 RTE_LOG(ERR, CHANNEL_MANAGER, "No Active Domains Running\n");
587 for (i = 0; i < *num_vm; i++) {
589 /* Get Domain Names */
590 vm_name = virDomainGetName(domptr[i]);
591 lvm_info[i].vm_name = vm_name;
593 /* Get Number of Vcpus */
594 numVcpus[i] = virDomainGetVcpusFlags(domptr[i], domain_flag);
596 /* Get Number of VCpus & VcpuPinInfo */
597 n_vcpus = virDomainGetVcpuPinInfo(domptr[i],
598 numVcpus[i], global_cpumaps,
599 global_maplen, domain_flag);
601 if ((int)n_vcpus > 0) {
603 lvm_info[i].num_cpus = n_vcpus;
606 /* Save pcpu in use by libvirt VMs */
607 for (ii = 0; ii < n_vcpus; ii++) {
609 for (jj = 0; jj < global_n_host_cpus; jj++) {
610 if (VIR_CPU_USABLE(global_cpumaps,
611 global_maplen, ii, jj) > 0) {
615 ITERATIVE_BITMASK_CHECK_64(mask, cpu) {
616 lvm_info[i].pcpus[ii] = cpu;
623 get_info_vm(const char *vm_name, struct vm_info *info)
625 struct virtual_machine_info *vm_info;
626 unsigned i, channel_num = 0;
629 vm_info = find_domain_by_name(vm_name);
630 if (vm_info == NULL) {
631 RTE_LOG(ERR, CHANNEL_MANAGER, "VM '%s' not found\n", vm_name);
634 info->status = CHANNEL_MGR_VM_ACTIVE;
635 if (!virDomainIsActive(vm_info->domainPtr))
636 info->status = CHANNEL_MGR_VM_INACTIVE;
638 rte_spinlock_lock(&(vm_info->config_spinlock));
640 mask = vm_info->channel_mask;
641 ITERATIVE_BITMASK_CHECK_64(mask, i) {
642 info->channels[channel_num].channel_num = i;
643 memcpy(info->channels[channel_num].channel_path,
644 vm_info->channels[i]->channel_path, UNIX_PATH_MAX);
645 info->channels[channel_num].status = vm_info->channels[i]->status;
646 info->channels[channel_num].fd = vm_info->channels[i]->fd;
650 info->num_channels = channel_num;
651 info->num_vcpus = vm_info->info.nrVirtCpu;
652 rte_spinlock_unlock(&(vm_info->config_spinlock));
654 memcpy(info->name, vm_info->name, sizeof(vm_info->name));
655 for (i = 0; i < info->num_vcpus; i++) {
656 info->pcpu_mask[i] = rte_atomic64_read(&vm_info->pcpu_mask[i]);
662 add_vm(const char *vm_name)
664 struct virtual_machine_info *new_domain;
665 virDomainPtr dom_ptr;
668 if (find_domain_by_name(vm_name) != NULL) {
669 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add VM: VM '%s' "
670 "already exists\n", vm_name);
674 if (global_vir_conn_ptr == NULL) {
675 RTE_LOG(ERR, CHANNEL_MANAGER, "No connection to hypervisor exists\n");
678 dom_ptr = virDomainLookupByName(global_vir_conn_ptr, vm_name);
679 if (dom_ptr == NULL) {
680 RTE_LOG(ERR, CHANNEL_MANAGER, "Error on VM lookup with libvirt: "
681 "VM '%s' not found\n", vm_name);
685 new_domain = rte_malloc("virtual_machine_info", sizeof(*new_domain),
686 RTE_CACHE_LINE_SIZE);
687 if (new_domain == NULL) {
688 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to allocate memory for VM "
692 new_domain->domainPtr = dom_ptr;
693 if (virDomainGetInfo(new_domain->domainPtr, &new_domain->info) != 0) {
694 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to get libvirt VM info\n");
695 rte_free(new_domain);
698 if (new_domain->info.nrVirtCpu > CHANNEL_CMDS_MAX_CPUS) {
699 RTE_LOG(ERR, CHANNEL_MANAGER, "Error the number of virtual CPUs(%u) is "
700 "greater than allowable(%d)\n", new_domain->info.nrVirtCpu,
701 CHANNEL_CMDS_MAX_CPUS);
702 rte_free(new_domain);
706 for (i = 0; i < CHANNEL_CMDS_MAX_CPUS; i++) {
707 rte_atomic64_init(&new_domain->pcpu_mask[i]);
709 if (update_pcpus_mask(new_domain) < 0) {
710 RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting physical CPU pinning\n");
711 rte_free(new_domain);
714 strncpy(new_domain->name, vm_name, sizeof(new_domain->name));
715 new_domain->name[sizeof(new_domain->name) - 1] = '\0';
716 new_domain->channel_mask = 0;
717 new_domain->num_channels = 0;
719 if (!virDomainIsActive(dom_ptr))
720 new_domain->status = CHANNEL_MGR_VM_INACTIVE;
722 new_domain->status = CHANNEL_MGR_VM_ACTIVE;
724 rte_spinlock_init(&(new_domain->config_spinlock));
725 LIST_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
730 remove_vm(const char *vm_name)
732 struct virtual_machine_info *vm_info = find_domain_by_name(vm_name);
734 if (vm_info == NULL) {
735 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to remove VM: VM '%s' "
736 "not found\n", vm_name);
739 rte_spinlock_lock(&vm_info->config_spinlock);
740 if (vm_info->num_channels != 0) {
741 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to remove VM '%s', there are "
742 "%"PRId8" channels still active\n",
743 vm_name, vm_info->num_channels);
744 rte_spinlock_unlock(&vm_info->config_spinlock);
747 LIST_REMOVE(vm_info, vms_info);
748 rte_spinlock_unlock(&vm_info->config_spinlock);
754 disconnect_hypervisor(void)
756 if (global_vir_conn_ptr != NULL) {
757 virConnectClose(global_vir_conn_ptr);
758 global_vir_conn_ptr = NULL;
763 connect_hypervisor(const char *path)
765 if (global_vir_conn_ptr != NULL) {
766 RTE_LOG(ERR, CHANNEL_MANAGER, "Error connecting to %s, connection "
767 "already established\n", path);
770 global_vir_conn_ptr = virConnectOpen(path);
771 if (global_vir_conn_ptr == NULL) {
772 RTE_LOG(ERR, CHANNEL_MANAGER, "Error failed to open connection to "
773 "Hypervisor '%s'\n", path);
779 channel_manager_init(const char *path __rte_unused)
783 LIST_INIT(&vm_list_head);
784 if (connect_hypervisor(path) < 0) {
785 global_n_host_cpus = 64;
786 global_hypervisor_available = 0;
787 RTE_LOG(INFO, CHANNEL_MANAGER, "Unable to initialize channel manager\n");
789 global_hypervisor_available = 1;
791 global_maplen = VIR_CPU_MAPLEN(CHANNEL_CMDS_MAX_CPUS);
793 global_vircpuinfo = rte_zmalloc(NULL,
794 sizeof(*global_vircpuinfo) *
795 CHANNEL_CMDS_MAX_CPUS, RTE_CACHE_LINE_SIZE);
796 if (global_vircpuinfo == NULL) {
797 RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for CPU Info\n");
800 global_cpumaps = rte_zmalloc(NULL,
801 CHANNEL_CMDS_MAX_CPUS * global_maplen,
802 RTE_CACHE_LINE_SIZE);
803 if (global_cpumaps == NULL)
806 if (virNodeGetInfo(global_vir_conn_ptr, &info)) {
807 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to retrieve node Info\n");
810 global_n_host_cpus = (unsigned int)info.cpus;
815 if (global_n_host_cpus > CHANNEL_CMDS_MAX_CPUS) {
816 RTE_LOG(WARNING, CHANNEL_MANAGER, "The number of host CPUs(%u) exceeds the "
817 "maximum of %u. No cores over %u should be used.\n",
818 global_n_host_cpus, CHANNEL_CMDS_MAX_CPUS,
819 CHANNEL_CMDS_MAX_CPUS - 1);
820 global_n_host_cpus = CHANNEL_CMDS_MAX_CPUS;
825 if (global_hypervisor_available)
826 disconnect_hypervisor();
831 channel_manager_exit(void)
835 struct virtual_machine_info *vm_info;
837 LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
839 rte_spinlock_lock(&(vm_info->config_spinlock));
841 mask = vm_info->channel_mask;
842 ITERATIVE_BITMASK_CHECK_64(mask, i) {
843 remove_channel_from_monitor(vm_info->channels[i]);
844 close(vm_info->channels[i]->fd);
845 rte_free(vm_info->channels[i]);
847 rte_spinlock_unlock(&(vm_info->config_spinlock));
849 LIST_REMOVE(vm_info, vms_info);
853 if (global_hypervisor_available) {
854 /* Only needed if hypervisor available */
855 rte_free(global_cpumaps);
856 rte_free(global_vircpuinfo);
857 disconnect_hypervisor();