0f5f9e287f7cae45e6e55bac1a1bf2b2be94a844
[dpdk.git] / examples / vm_power_manager / channel_manager.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/un.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <inttypes.h>
11 #include <dirent.h>
12 #include <errno.h>
13
14 #include <sys/queue.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/socket.h>
18 #include <sys/select.h>
19
20 #include <rte_string_fns.h>
21 #include <rte_malloc.h>
22 #include <rte_memory.h>
23 #include <rte_mempool.h>
24 #include <rte_log.h>
25 #include <rte_atomic.h>
26 #include <rte_spinlock.h>
27
28 #include <libvirt/libvirt.h>
29
30 #include "channel_manager.h"
31 #include "channel_commands.h"
32 #include "channel_monitor.h"
33 #include "power_manager.h"
34
35
36 #define RTE_LOGTYPE_CHANNEL_MANAGER RTE_LOGTYPE_USER1
37
38 /* Global pointer to libvirt connection */
39 static virConnectPtr global_vir_conn_ptr;
40
41 static unsigned char *global_cpumaps;
42 static virVcpuInfo *global_vircpuinfo;
43 static size_t global_maplen;
44
45 static unsigned int global_n_host_cpus;
46 static bool global_hypervisor_available;
47
48 /*
49  * Represents a single Virtual Machine
50  */
51 struct virtual_machine_info {
52         char name[CHANNEL_MGR_MAX_NAME_LEN];
53         uint16_t pcpu_map[RTE_MAX_LCORE];
54         struct channel_info *channels[RTE_MAX_LCORE];
55         char channel_mask[RTE_MAX_LCORE];
56         uint8_t num_channels;
57         enum vm_status status;
58         virDomainPtr domainPtr;
59         virDomainInfo info;
60         rte_spinlock_t config_spinlock;
61         LIST_ENTRY(virtual_machine_info) vms_info;
62 };
63
64 LIST_HEAD(, virtual_machine_info) vm_list_head;
65
66 static struct virtual_machine_info *
67 find_domain_by_name(const char *name)
68 {
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))
72                         return info;
73         }
74         return NULL;
75 }
76
77 static int
78 update_pcpus_mask(struct virtual_machine_info *vm_info)
79 {
80         virVcpuInfoPtr cpuinfo;
81         unsigned i, j;
82         int n_vcpus;
83
84         memset(global_cpumaps, 0, RTE_MAX_LCORE*global_maplen);
85
86         if (!virDomainIsActive(vm_info->domainPtr)) {
87                 n_vcpus = virDomainGetVcpuPinInfo(vm_info->domainPtr,
88                                 vm_info->info.nrVirtCpu, global_cpumaps, global_maplen,
89                                 VIR_DOMAIN_AFFECT_CONFIG);
90                 if (n_vcpus < 0) {
91                         RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting vCPU info for "
92                                         "in-active VM '%s'\n", vm_info->name);
93                         return -1;
94                 }
95                 goto update_pcpus;
96         }
97
98         memset(global_vircpuinfo, 0, sizeof(*global_vircpuinfo)*
99                         RTE_MAX_LCORE);
100
101         cpuinfo = global_vircpuinfo;
102
103         n_vcpus = virDomainGetVcpus(vm_info->domainPtr, cpuinfo,
104                         RTE_MAX_LCORE, global_cpumaps, global_maplen);
105         if (n_vcpus < 0) {
106                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting vCPU info for "
107                                 "active VM '%s'\n", vm_info->name);
108                 return -1;
109         }
110 update_pcpus:
111         if (n_vcpus >= RTE_MAX_LCORE) {
112                 RTE_LOG(ERR, CHANNEL_MANAGER, "Number of vCPUS(%u) is out of range "
113                                 "0...%d\n", n_vcpus, RTE_MAX_LCORE-1);
114                 return -1;
115         }
116         if (n_vcpus != vm_info->info.nrVirtCpu) {
117                 RTE_LOG(INFO, CHANNEL_MANAGER, "Updating the number of vCPUs for VM '%s"
118                                 " from %d -> %d\n", vm_info->name, vm_info->info.nrVirtCpu,
119                                 n_vcpus);
120                 vm_info->info.nrVirtCpu = n_vcpus;
121         }
122         rte_spinlock_lock(&(vm_info->config_spinlock));
123         for (i = 0; i < vm_info->info.nrVirtCpu; i++) {
124                 for (j = 0; j < global_n_host_cpus; j++) {
125                         if (VIR_CPU_USABLE(global_cpumaps,
126                                         global_maplen, i, j) <= 0)
127                                 continue;
128                         vm_info->pcpu_map[i] = j;
129                 }
130         }
131         rte_spinlock_unlock(&(vm_info->config_spinlock));
132         return 0;
133 }
134
135 int
136 set_pcpu(char *vm_name, unsigned int vcpu, unsigned int pcpu)
137 {
138         int flags = VIR_DOMAIN_AFFECT_LIVE|VIR_DOMAIN_AFFECT_CONFIG;
139         struct virtual_machine_info *vm_info;
140
141         if (vcpu >= RTE_MAX_LCORE) {
142                 RTE_LOG(ERR, CHANNEL_MANAGER, "vCPU(%u) exceeds max allowable(%d)\n",
143                                 vcpu, RTE_MAX_LCORE-1);
144                 return -1;
145         }
146
147         vm_info = find_domain_by_name(vm_name);
148         if (vm_info == NULL) {
149                 RTE_LOG(ERR, CHANNEL_MANAGER, "VM '%s' not found\n", vm_name);
150                 return -1;
151         }
152
153         if (!virDomainIsActive(vm_info->domainPtr)) {
154                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to set vCPU(%u) to pCPU "
155                                 " for VM '%s', VM is not active\n",
156                                 vcpu, vm_info->name);
157                 return -1;
158         }
159
160         if (vcpu >= vm_info->info.nrVirtCpu) {
161                 RTE_LOG(ERR, CHANNEL_MANAGER, "vCPU(%u) exceeds the assigned number of "
162                                 "vCPUs(%u)\n", vcpu, vm_info->info.nrVirtCpu);
163                 return -1;
164         }
165         memset(global_cpumaps, 0, RTE_MAX_LCORE * global_maplen);
166
167         VIR_USE_CPU(global_cpumaps, pcpu);
168
169         if (pcpu >= global_n_host_cpus) {
170                 RTE_LOG(ERR, CHANNEL_MANAGER, "CPU(%u) exceeds the available "
171                                 "number of CPUs(%u)\n",
172                                 pcpu, global_n_host_cpus);
173                 return -1;
174         }
175
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                                 " for VM '%s'\n", vcpu,
180                                 vm_info->name);
181                 return -1;
182         }
183         rte_spinlock_lock(&(vm_info->config_spinlock));
184         vm_info->pcpu_map[vcpu] = pcpu;
185         rte_spinlock_unlock(&(vm_info->config_spinlock));
186         return 0;
187 }
188
189 uint16_t
190 get_pcpu(struct channel_info *chan_info, unsigned int vcpu)
191 {
192         struct virtual_machine_info *vm_info =
193                         (struct virtual_machine_info *)chan_info->priv_info;
194
195         if (global_hypervisor_available && (vm_info != NULL)) {
196                 uint16_t pcpu;
197                 rte_spinlock_lock(&(vm_info->config_spinlock));
198                 pcpu = vm_info->pcpu_map[vcpu];
199                 rte_spinlock_unlock(&(vm_info->config_spinlock));
200                 return pcpu;
201         } else
202                 return 0;
203 }
204
205 static inline int
206 channel_exists(struct virtual_machine_info *vm_info, unsigned channel_num)
207 {
208         rte_spinlock_lock(&(vm_info->config_spinlock));
209         if (vm_info->channel_mask[channel_num] == 1) {
210                 rte_spinlock_unlock(&(vm_info->config_spinlock));
211                 return 1;
212         }
213         rte_spinlock_unlock(&(vm_info->config_spinlock));
214         return 0;
215 }
216
217
218
219 static int
220 open_non_blocking_channel(struct channel_info *info)
221 {
222         int ret, flags;
223         struct sockaddr_un sock_addr;
224         fd_set soc_fd_set;
225         struct timeval tv;
226
227         info->fd = socket(AF_UNIX, SOCK_STREAM, 0);
228         if (info->fd < 0) {
229                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error(%s) creating socket for '%s'\n",
230                                 strerror(errno),
231                                 info->channel_path);
232                 return -1;
233         }
234         sock_addr.sun_family = AF_UNIX;
235         memcpy(&sock_addr.sun_path, info->channel_path,
236                         strlen(info->channel_path)+1);
237
238         /* Get current flags */
239         flags = fcntl(info->fd, F_GETFL, 0);
240         if (flags < 0) {
241                 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) fcntl get flags socket for"
242                                 "'%s'\n", strerror(errno), info->channel_path);
243                 return 1;
244         }
245         /* Set to Non Blocking */
246         flags |= O_NONBLOCK;
247         if (fcntl(info->fd, F_SETFL, flags) < 0) {
248                 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) setting non-blocking "
249                                 "socket for '%s'\n", strerror(errno), info->channel_path);
250                 return -1;
251         }
252         ret = connect(info->fd, (struct sockaddr *)&sock_addr,
253                         sizeof(sock_addr));
254         if (ret < 0) {
255                 /* ECONNREFUSED error is given when VM is not active */
256                 if (errno == ECONNREFUSED) {
257                         RTE_LOG(WARNING, CHANNEL_MANAGER, "VM is not active or has not "
258                                         "activated its endpoint to channel %s\n",
259                                         info->channel_path);
260                         return -1;
261                 }
262                 /* Wait for tv_sec if in progress */
263                 else if (errno == EINPROGRESS) {
264                         tv.tv_sec = 2;
265                         tv.tv_usec = 0;
266                         FD_ZERO(&soc_fd_set);
267                         FD_SET(info->fd, &soc_fd_set);
268                         if (select(info->fd+1, NULL, &soc_fd_set, NULL, &tv) > 0) {
269                                 RTE_LOG(WARNING, CHANNEL_MANAGER, "Timeout or error on channel "
270                                                 "'%s'\n", info->channel_path);
271                                 return -1;
272                         }
273                 } else {
274                         /* Any other error */
275                         RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) connecting socket"
276                                         " for '%s'\n", strerror(errno), info->channel_path);
277                         return -1;
278                 }
279         }
280         return 0;
281 }
282
283 static int
284 open_host_channel(struct channel_info *info)
285 {
286         int flags;
287
288         info->fd = open(info->channel_path, O_RDWR | O_RSYNC);
289         if (info->fd < 0) {
290                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error(%s) opening fifo for '%s'\n",
291                                 strerror(errno),
292                                 info->channel_path);
293                 return -1;
294         }
295
296         /* Get current flags */
297         flags = fcntl(info->fd, F_GETFL, 0);
298         if (flags < 0) {
299                 RTE_LOG(WARNING, CHANNEL_MANAGER, "Error(%s) fcntl get flags socket for"
300                                 "'%s'\n", strerror(errno), info->channel_path);
301                 return 1;
302         }
303         /* Set to Non Blocking */
304         flags |= O_NONBLOCK;
305         if (fcntl(info->fd, F_SETFL, flags) < 0) {
306                 RTE_LOG(WARNING, CHANNEL_MANAGER,
307                                 "Error(%s) setting non-blocking "
308                                 "socket for '%s'\n",
309                                 strerror(errno), info->channel_path);
310                 return -1;
311         }
312         return 0;
313 }
314
315 static int
316 setup_channel_info(struct virtual_machine_info **vm_info_dptr,
317                 struct channel_info **chan_info_dptr, unsigned channel_num)
318 {
319         struct channel_info *chan_info = *chan_info_dptr;
320         struct virtual_machine_info *vm_info = *vm_info_dptr;
321
322         chan_info->channel_num = channel_num;
323         chan_info->priv_info = (void *)vm_info;
324         chan_info->status = CHANNEL_MGR_CHANNEL_DISCONNECTED;
325         chan_info->type = CHANNEL_TYPE_BINARY;
326         if (open_non_blocking_channel(chan_info) < 0) {
327                 RTE_LOG(ERR, CHANNEL_MANAGER, "Could not open channel: "
328                                 "'%s' for VM '%s'\n",
329                                 chan_info->channel_path, vm_info->name);
330                 return -1;
331         }
332         if (add_channel_to_monitor(&chan_info) < 0) {
333                 RTE_LOG(ERR, CHANNEL_MANAGER, "Could add channel: "
334                                 "'%s' to epoll ctl for VM '%s'\n",
335                                 chan_info->channel_path, vm_info->name);
336                 return -1;
337
338         }
339         rte_spinlock_lock(&(vm_info->config_spinlock));
340         vm_info->num_channels++;
341         vm_info->channel_mask[channel_num] = 1;
342         vm_info->channels[channel_num] = chan_info;
343         chan_info->status = CHANNEL_MGR_CHANNEL_CONNECTED;
344         rte_spinlock_unlock(&(vm_info->config_spinlock));
345         return 0;
346 }
347
348 static void
349 fifo_path(char *dst, unsigned int len)
350 {
351         snprintf(dst, len, "%sfifo", CHANNEL_MGR_SOCKET_PATH);
352 }
353
354 static int
355 setup_host_channel_info(struct channel_info **chan_info_dptr,
356                 unsigned int channel_num)
357 {
358         struct channel_info *chan_info = *chan_info_dptr;
359
360         chan_info->channel_num = channel_num;
361         chan_info->priv_info = (void *)NULL;
362         chan_info->status = CHANNEL_MGR_CHANNEL_DISCONNECTED;
363         chan_info->type = CHANNEL_TYPE_JSON;
364
365         if (open_host_channel(chan_info) < 0) {
366                 RTE_LOG(ERR, CHANNEL_MANAGER, "Could not open host channel: "
367                                 "'%s'\n",
368                                 chan_info->channel_path);
369                 return -1;
370         }
371         if (add_channel_to_monitor(&chan_info) < 0) {
372                 RTE_LOG(ERR, CHANNEL_MANAGER, "Could add channel: "
373                                 "'%s' to epoll ctl\n",
374                                 chan_info->channel_path);
375                 return -1;
376
377         }
378         chan_info->status = CHANNEL_MGR_CHANNEL_CONNECTED;
379         return 0;
380 }
381
382 int
383 add_all_channels(const char *vm_name)
384 {
385         DIR *d;
386         struct dirent *dir;
387         struct virtual_machine_info *vm_info;
388         struct channel_info *chan_info;
389         char *token, *remaining, *tail_ptr;
390         char socket_name[PATH_MAX];
391         unsigned channel_num;
392         int num_channels_enabled = 0;
393
394         /* verify VM exists */
395         vm_info = find_domain_by_name(vm_name);
396         if (vm_info == NULL) {
397                 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' not found"
398                                 " during channel discovery\n", vm_name);
399                 return 0;
400         }
401         if (!virDomainIsActive(vm_info->domainPtr)) {
402                 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' is not active\n", vm_name);
403                 vm_info->status = CHANNEL_MGR_VM_INACTIVE;
404                 return 0;
405         }
406         d = opendir(CHANNEL_MGR_SOCKET_PATH);
407         if (d == NULL) {
408                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error opening directory '%s': %s\n",
409                                 CHANNEL_MGR_SOCKET_PATH, strerror(errno));
410                 return -1;
411         }
412         while ((dir = readdir(d)) != NULL) {
413                 if (!strncmp(dir->d_name, ".", 1) ||
414                                 !strncmp(dir->d_name, "..", 2))
415                         continue;
416
417                 strlcpy(socket_name, dir->d_name, sizeof(socket_name));
418                 remaining = socket_name;
419                 /* Extract vm_name from "<vm_name>.<channel_num>" */
420                 token = strsep(&remaining, ".");
421                 if (remaining == NULL)
422                         continue;
423                 if (strncmp(vm_name, token, CHANNEL_MGR_MAX_NAME_LEN))
424                         continue;
425
426                 /* remaining should contain only <channel_num> */
427                 errno = 0;
428                 channel_num = (unsigned)strtol(remaining, &tail_ptr, 0);
429                 if ((errno != 0) || (remaining[0] == '\0') ||
430                                 tail_ptr == NULL || (*tail_ptr != '\0')) {
431                         RTE_LOG(WARNING, CHANNEL_MANAGER, "Malformed channel name"
432                                         "'%s' found it should be in the form of "
433                                         "'<guest_name>.<channel_num>(decimal)'\n",
434                                         dir->d_name);
435                         continue;
436                 }
437                 if (channel_num >= RTE_MAX_LCORE) {
438                         RTE_LOG(WARNING, CHANNEL_MANAGER, "Channel number(%u) is "
439                                         "greater than max allowable: %d, skipping '%s%s'\n",
440                                         channel_num, RTE_MAX_LCORE-1,
441                                         CHANNEL_MGR_SOCKET_PATH, dir->d_name);
442                         continue;
443                 }
444                 /* if channel has not been added previously */
445                 if (channel_exists(vm_info, channel_num))
446                         continue;
447
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%s'\n", CHANNEL_MGR_SOCKET_PATH, dir->d_name);
453                         continue;
454                 }
455
456                 snprintf(chan_info->channel_path,
457                                 sizeof(chan_info->channel_path), "%s%s",
458                                 CHANNEL_MGR_SOCKET_PATH, dir->d_name);
459
460                 if (setup_channel_info(&vm_info, &chan_info, channel_num) < 0) {
461                         rte_free(chan_info);
462                         continue;
463                 }
464
465                 num_channels_enabled++;
466         }
467         closedir(d);
468         return num_channels_enabled;
469 }
470
471 int
472 add_channels(const char *vm_name, unsigned *channel_list,
473                 unsigned len_channel_list)
474 {
475         struct virtual_machine_info *vm_info;
476         struct channel_info *chan_info;
477         char socket_path[PATH_MAX];
478         unsigned i;
479         int num_channels_enabled = 0;
480
481         vm_info = find_domain_by_name(vm_name);
482         if (vm_info == NULL) {
483                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add channels: VM '%s' "
484                                 "not found\n", vm_name);
485                 return 0;
486         }
487
488         if (!virDomainIsActive(vm_info->domainPtr)) {
489                 RTE_LOG(ERR, CHANNEL_MANAGER, "VM: '%s' is not active\n", vm_name);
490                 vm_info->status = CHANNEL_MGR_VM_INACTIVE;
491                 return 0;
492         }
493
494         for (i = 0; i < len_channel_list; i++) {
495
496                 if (channel_list[i] >= RTE_MAX_LCORE) {
497                         RTE_LOG(INFO, CHANNEL_MANAGER, "Channel(%u) is out of range "
498                                                         "0...%d\n", channel_list[i],
499                                                         RTE_MAX_LCORE-1);
500                         continue;
501                 }
502                 if (channel_exists(vm_info, channel_list[i])) {
503                         RTE_LOG(INFO, CHANNEL_MANAGER, "Channel already exists, skipping  "
504                                         "'%s.%u'\n", vm_name, i);
505                         continue;
506                 }
507
508                 snprintf(socket_path, sizeof(socket_path), "%s%s.%u",
509                                 CHANNEL_MGR_SOCKET_PATH, vm_name, channel_list[i]);
510                 errno = 0;
511                 if (access(socket_path, F_OK) < 0) {
512                         RTE_LOG(ERR, CHANNEL_MANAGER, "Channel path '%s' error: "
513                                         "%s\n", socket_path, strerror(errno));
514                         continue;
515                 }
516                 chan_info = rte_malloc(NULL, sizeof(*chan_info),
517                                 RTE_CACHE_LINE_SIZE);
518                 if (chan_info == NULL) {
519                         RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for "
520                                         "channel '%s'\n", socket_path);
521                         continue;
522                 }
523                 snprintf(chan_info->channel_path,
524                                 sizeof(chan_info->channel_path), "%s%s.%u",
525                                 CHANNEL_MGR_SOCKET_PATH, vm_name, channel_list[i]);
526                 if (setup_channel_info(&vm_info, &chan_info, channel_list[i]) < 0) {
527                         rte_free(chan_info);
528                         continue;
529                 }
530                 num_channels_enabled++;
531
532         }
533         return num_channels_enabled;
534 }
535
536 int
537 add_host_channel(void)
538 {
539         struct channel_info *chan_info;
540         char socket_path[PATH_MAX];
541         int num_channels_enabled = 0;
542         int ret;
543
544         fifo_path(socket_path, sizeof(socket_path));
545
546         ret = mkfifo(socket_path, 0660);
547         if ((errno != EEXIST) && (ret < 0)) {
548                 RTE_LOG(ERR, CHANNEL_MANAGER, "Cannot create fifo '%s' error: "
549                                 "%s\n", socket_path, strerror(errno));
550                 return 0;
551         }
552
553         if (access(socket_path, F_OK) < 0) {
554                 RTE_LOG(ERR, CHANNEL_MANAGER, "Channel path '%s' error: "
555                                 "%s\n", socket_path, strerror(errno));
556                 return 0;
557         }
558         chan_info = rte_malloc(NULL, sizeof(*chan_info), 0);
559         if (chan_info == NULL) {
560                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for "
561                                 "channel '%s'\n", socket_path);
562                 return 0;
563         }
564         rte_strlcpy(chan_info->channel_path, socket_path, UNIX_PATH_MAX);
565
566         if (setup_host_channel_info(&chan_info, 0) < 0) {
567                 rte_free(chan_info);
568                 return 0;
569         }
570         num_channels_enabled++;
571
572         return num_channels_enabled;
573 }
574
575 int
576 remove_channel(struct channel_info **chan_info_dptr)
577 {
578         struct virtual_machine_info *vm_info;
579         struct channel_info *chan_info = *chan_info_dptr;
580
581         close(chan_info->fd);
582
583         vm_info = (struct virtual_machine_info *)chan_info->priv_info;
584
585         rte_spinlock_lock(&(vm_info->config_spinlock));
586         vm_info->channel_mask[chan_info->channel_num] = 0;
587         vm_info->num_channels--;
588         rte_spinlock_unlock(&(vm_info->config_spinlock));
589
590         rte_free(chan_info);
591         return 0;
592 }
593
594 int
595 set_channel_status_all(const char *vm_name, enum channel_status status)
596 {
597         struct virtual_machine_info *vm_info;
598         unsigned i;
599         char mask[RTE_MAX_LCORE];
600         int num_channels_changed = 0;
601
602         if (!(status == CHANNEL_MGR_CHANNEL_CONNECTED ||
603                         status == CHANNEL_MGR_CHANNEL_DISABLED)) {
604                 RTE_LOG(ERR, CHANNEL_MANAGER, "Channels can only be enabled or "
605                                 "disabled: Unable to change status for VM '%s'\n", vm_name);
606         }
607         vm_info = find_domain_by_name(vm_name);
608         if (vm_info == NULL) {
609                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to disable channels: VM '%s' "
610                                 "not found\n", vm_name);
611                 return 0;
612         }
613
614         rte_spinlock_lock(&(vm_info->config_spinlock));
615         memcpy(mask, (char *)vm_info->channel_mask, RTE_MAX_LCORE);
616         for (i = 0; i < RTE_MAX_LCORE; i++) {
617                 if (mask[i] != 1)
618                         continue;
619                 vm_info->channels[i]->status = status;
620                 num_channels_changed++;
621         }
622         rte_spinlock_unlock(&(vm_info->config_spinlock));
623         return num_channels_changed;
624
625 }
626
627 int
628 set_channel_status(const char *vm_name, unsigned *channel_list,
629                 unsigned len_channel_list, enum channel_status status)
630 {
631         struct virtual_machine_info *vm_info;
632         unsigned i;
633         int num_channels_changed = 0;
634
635         if (!(status == CHANNEL_MGR_CHANNEL_CONNECTED ||
636                         status == CHANNEL_MGR_CHANNEL_DISABLED)) {
637                 RTE_LOG(ERR, CHANNEL_MANAGER, "Channels can only be enabled or "
638                                 "disabled: Unable to change status for VM '%s'\n", vm_name);
639         }
640         vm_info = find_domain_by_name(vm_name);
641         if (vm_info == NULL) {
642                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add channels: VM '%s' "
643                                 "not found\n", vm_name);
644                 return 0;
645         }
646         for (i = 0; i < len_channel_list; i++) {
647                 if (channel_exists(vm_info, channel_list[i])) {
648                         rte_spinlock_lock(&(vm_info->config_spinlock));
649                         vm_info->channels[channel_list[i]]->status = status;
650                         rte_spinlock_unlock(&(vm_info->config_spinlock));
651                         num_channels_changed++;
652                 }
653         }
654         return num_channels_changed;
655 }
656
657 void
658 get_all_vm(int *num_vm, int *num_vcpu)
659 {
660
661         virNodeInfo node_info;
662         virDomainPtr *domptr;
663         int i, ii, numVcpus[MAX_VCPUS], n_vcpus;
664         unsigned int jj;
665         const char *vm_name;
666         unsigned int domain_flags = VIR_CONNECT_LIST_DOMAINS_RUNNING |
667                                 VIR_CONNECT_LIST_DOMAINS_PERSISTENT;
668         unsigned int domain_flag = VIR_DOMAIN_VCPU_CONFIG;
669
670         if (!global_hypervisor_available)
671                 return;
672
673         memset(global_cpumaps, 0, RTE_MAX_LCORE*global_maplen);
674         if (virNodeGetInfo(global_vir_conn_ptr, &node_info)) {
675                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to retrieve node Info\n");
676                 return;
677         }
678
679         /* Returns number of pcpus */
680         global_n_host_cpus = (unsigned int)node_info.cpus;
681
682         /* Returns number of active domains */
683         *num_vm = virConnectListAllDomains(global_vir_conn_ptr, &domptr,
684                                         domain_flags);
685         if (*num_vm <= 0) {
686                 RTE_LOG(ERR, CHANNEL_MANAGER, "No Active Domains Running\n");
687                 return;
688         }
689
690         for (i = 0; i < *num_vm; i++) {
691
692                 /* Get Domain Names */
693                 vm_name = virDomainGetName(domptr[i]);
694                 lvm_info[i].vm_name = vm_name;
695
696                 /* Get Number of Vcpus */
697                 numVcpus[i] = virDomainGetVcpusFlags(domptr[i], domain_flag);
698
699                 /* Get Number of VCpus & VcpuPinInfo */
700                 n_vcpus = virDomainGetVcpuPinInfo(domptr[i],
701                                 numVcpus[i], global_cpumaps,
702                                 global_maplen, domain_flag);
703
704                 if ((int)n_vcpus > 0) {
705                         *num_vcpu = n_vcpus;
706                         lvm_info[i].num_cpus = n_vcpus;
707                 }
708
709                 /* Save pcpu in use by libvirt VMs */
710                 for (ii = 0; ii < n_vcpus; ii++) {
711                         for (jj = 0; jj < global_n_host_cpus; jj++) {
712                                 if (VIR_CPU_USABLE(global_cpumaps,
713                                                 global_maplen, ii, jj) > 0) {
714                                         lvm_info[i].pcpus[ii] = jj;
715                                 }
716                         }
717                 }
718         }
719 }
720
721 int
722 get_info_vm(const char *vm_name, struct vm_info *info)
723 {
724         struct virtual_machine_info *vm_info;
725         unsigned i, channel_num = 0;
726         char mask[RTE_MAX_LCORE];
727
728         vm_info = find_domain_by_name(vm_name);
729         if (vm_info == NULL) {
730                 RTE_LOG(ERR, CHANNEL_MANAGER, "VM '%s' not found\n", vm_name);
731                 return -1;
732         }
733         info->status = CHANNEL_MGR_VM_ACTIVE;
734         if (!virDomainIsActive(vm_info->domainPtr))
735                 info->status = CHANNEL_MGR_VM_INACTIVE;
736
737         rte_spinlock_lock(&(vm_info->config_spinlock));
738
739         memcpy(mask, (char *)vm_info->channel_mask, RTE_MAX_LCORE);
740         for (i = 0; i < RTE_MAX_LCORE; i++) {
741                 if (mask[i] != 1)
742                         continue;
743                 info->channels[channel_num].channel_num = i;
744                 memcpy(info->channels[channel_num].channel_path,
745                                 vm_info->channels[i]->channel_path,
746                                 UNIX_PATH_MAX);
747                 info->channels[channel_num].status =
748                                 vm_info->channels[i]->status;
749                 info->channels[channel_num].fd =
750                                 vm_info->channels[i]->fd;
751                 channel_num++;
752         }
753
754         info->num_channels = channel_num;
755         info->num_vcpus = vm_info->info.nrVirtCpu;
756         rte_spinlock_unlock(&(vm_info->config_spinlock));
757
758         memcpy(info->name, vm_info->name, sizeof(vm_info->name));
759         rte_spinlock_lock(&(vm_info->config_spinlock));
760         for (i = 0; i < info->num_vcpus; i++) {
761                 info->pcpu_map[i] = vm_info->pcpu_map[i];
762         }
763         rte_spinlock_unlock(&(vm_info->config_spinlock));
764         return 0;
765 }
766
767 int
768 add_vm(const char *vm_name)
769 {
770         struct virtual_machine_info *new_domain;
771         virDomainPtr dom_ptr;
772         int i;
773
774         if (find_domain_by_name(vm_name) != NULL) {
775                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to add VM: VM '%s' "
776                                 "already exists\n", vm_name);
777                 return -1;
778         }
779
780         if (global_vir_conn_ptr == NULL) {
781                 RTE_LOG(ERR, CHANNEL_MANAGER, "No connection to hypervisor exists\n");
782                 return -1;
783         }
784         dom_ptr = virDomainLookupByName(global_vir_conn_ptr, vm_name);
785         if (dom_ptr == NULL) {
786                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error on VM lookup with libvirt: "
787                                 "VM '%s' not found\n", vm_name);
788                 return -1;
789         }
790
791         new_domain = rte_malloc("virtual_machine_info", sizeof(*new_domain),
792                         RTE_CACHE_LINE_SIZE);
793         if (new_domain == NULL) {
794                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to allocate memory for VM "
795                                 "info\n");
796                 return -1;
797         }
798         new_domain->domainPtr = dom_ptr;
799         if (virDomainGetInfo(new_domain->domainPtr, &new_domain->info) != 0) {
800                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to get libvirt VM info\n");
801                 rte_free(new_domain);
802                 return -1;
803         }
804         if (new_domain->info.nrVirtCpu > RTE_MAX_LCORE) {
805                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error the number of virtual CPUs(%u) is "
806                                 "greater than allowable(%d)\n", new_domain->info.nrVirtCpu,
807                                 RTE_MAX_LCORE);
808                 rte_free(new_domain);
809                 return -1;
810         }
811
812         for (i = 0; i < RTE_MAX_LCORE; i++)
813                 new_domain->pcpu_map[i] = 0;
814
815         if (update_pcpus_mask(new_domain) < 0) {
816                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error getting physical CPU pinning\n");
817                 rte_free(new_domain);
818                 return -1;
819         }
820         strncpy(new_domain->name, vm_name, sizeof(new_domain->name));
821         new_domain->name[sizeof(new_domain->name) - 1] = '\0';
822         memset(new_domain->channel_mask, 0, RTE_MAX_LCORE);
823         new_domain->num_channels = 0;
824
825         if (!virDomainIsActive(dom_ptr))
826                 new_domain->status = CHANNEL_MGR_VM_INACTIVE;
827         else
828                 new_domain->status = CHANNEL_MGR_VM_ACTIVE;
829
830         rte_spinlock_init(&(new_domain->config_spinlock));
831         LIST_INSERT_HEAD(&vm_list_head, new_domain, vms_info);
832         return 0;
833 }
834
835 int
836 remove_vm(const char *vm_name)
837 {
838         struct virtual_machine_info *vm_info = find_domain_by_name(vm_name);
839
840         if (vm_info == NULL) {
841                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to remove VM: VM '%s' "
842                                 "not found\n", vm_name);
843                 return -1;
844         }
845         rte_spinlock_lock(&vm_info->config_spinlock);
846         if (vm_info->num_channels != 0) {
847                 RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to remove VM '%s', there are "
848                                 "%"PRId8" channels still active\n",
849                                 vm_name, vm_info->num_channels);
850                 rte_spinlock_unlock(&vm_info->config_spinlock);
851                 return -1;
852         }
853         LIST_REMOVE(vm_info, vms_info);
854         rte_spinlock_unlock(&vm_info->config_spinlock);
855         rte_free(vm_info);
856         return 0;
857 }
858
859 static void
860 disconnect_hypervisor(void)
861 {
862         if (global_vir_conn_ptr != NULL) {
863                 virConnectClose(global_vir_conn_ptr);
864                 global_vir_conn_ptr = NULL;
865         }
866 }
867
868 static int
869 connect_hypervisor(const char *path)
870 {
871         if (global_vir_conn_ptr != NULL) {
872                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error connecting to %s, connection "
873                                 "already established\n", path);
874                 return -1;
875         }
876         global_vir_conn_ptr = virConnectOpen(path);
877         if (global_vir_conn_ptr == NULL) {
878                 RTE_LOG(ERR, CHANNEL_MANAGER, "Error failed to open connection to "
879                                 "Hypervisor '%s'\n", path);
880                 return -1;
881         }
882         return 0;
883 }
884 int
885 channel_manager_init(const char *path __rte_unused)
886 {
887         virNodeInfo info;
888
889         LIST_INIT(&vm_list_head);
890         if (connect_hypervisor(path) < 0) {
891                 global_n_host_cpus = 64;
892                 global_hypervisor_available = 0;
893                 RTE_LOG(INFO, CHANNEL_MANAGER, "Unable to initialize channel manager\n");
894         } else {
895                 global_hypervisor_available = 1;
896
897                 global_maplen = VIR_CPU_MAPLEN(RTE_MAX_LCORE);
898
899                 global_vircpuinfo = rte_zmalloc(NULL,
900                                 sizeof(*global_vircpuinfo) *
901                                 RTE_MAX_LCORE, RTE_CACHE_LINE_SIZE);
902                 if (global_vircpuinfo == NULL) {
903                         RTE_LOG(ERR, CHANNEL_MANAGER, "Error allocating memory for CPU Info\n");
904                         goto error;
905                 }
906                 global_cpumaps = rte_zmalloc(NULL,
907                                 RTE_MAX_LCORE * global_maplen,
908                                 RTE_CACHE_LINE_SIZE);
909                 if (global_cpumaps == NULL)
910                         goto error;
911
912                 if (virNodeGetInfo(global_vir_conn_ptr, &info)) {
913                         RTE_LOG(ERR, CHANNEL_MANAGER, "Unable to retrieve node Info\n");
914                         goto error;
915                 }
916                 global_n_host_cpus = (unsigned int)info.cpus;
917         }
918
919
920
921         if (global_n_host_cpus > RTE_MAX_LCORE) {
922                 RTE_LOG(WARNING, CHANNEL_MANAGER, "The number of host CPUs(%u) exceeds the "
923                                 "maximum of %u. No cores over %u should be used.\n",
924                                 global_n_host_cpus, RTE_MAX_LCORE,
925                                 RTE_MAX_LCORE - 1);
926                 global_n_host_cpus = RTE_MAX_LCORE;
927         }
928
929         return 0;
930 error:
931         if (global_hypervisor_available)
932                 disconnect_hypervisor();
933         return -1;
934 }
935
936 void
937 channel_manager_exit(void)
938 {
939         unsigned i;
940         char mask[RTE_MAX_LCORE];
941         struct virtual_machine_info *vm_info;
942
943         LIST_FOREACH(vm_info, &vm_list_head, vms_info) {
944
945                 rte_spinlock_lock(&(vm_info->config_spinlock));
946
947                 memcpy(mask, (char *)vm_info->channel_mask, RTE_MAX_LCORE);
948                 for (i = 0; i < RTE_MAX_LCORE; i++) {
949                         if (mask[i] != 1)
950                                 continue;
951                         remove_channel_from_monitor(
952                                         vm_info->channels[i]);
953                         close(vm_info->channels[i]->fd);
954                         rte_free(vm_info->channels[i]);
955                 }
956                 rte_spinlock_unlock(&(vm_info->config_spinlock));
957
958                 LIST_REMOVE(vm_info, vms_info);
959                 rte_free(vm_info);
960         }
961
962         if (global_hypervisor_available) {
963                 /* Only needed if hypervisor available */
964                 rte_free(global_cpumaps);
965                 rte_free(global_vircpuinfo);
966                 disconnect_hypervisor();
967         }
968 }