1 /*
2  * Copyright (C) 2016 EPAM Systems Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 
15 #include <stdlib.h>
16 
17 #include <libxl.h>
18 #include <libxl_utils.h>
19 #include <libxlutil.h>
20 
21 #include "xl.h"
22 #include "xl_utils.h"
23 #include "xl_parse.h"
24 
main_vdisplattach(int argc,char ** argv)25 int main_vdisplattach(int argc, char **argv)
26 {
27     int opt;
28     int rc;
29     uint32_t domid;
30     libxl_device_vdispl vdispl;
31 
32     SWITCH_FOREACH_OPT(opt, "", NULL, "vdispl-attach", 1) {
33         /* No options */
34     }
35 
36     libxl_device_vdispl_init(&vdispl);
37     domid = find_domain(argv[optind++]);
38 
39     for (argv += optind, argc -= optind; argc > 0; ++argv, --argc) {
40         rc = parse_vdispl_config(&vdispl, *argv);
41         if (rc) goto out;
42     }
43 
44     if (vdispl.num_connectors == 0) {
45         fprintf(stderr, "At least one connector should be specified.\n");
46         rc = ERROR_FAIL; goto out;
47     }
48 
49     if (dryrun_only) {
50         char *json = libxl_device_vdispl_to_json(ctx, &vdispl);
51         printf("vdispl: %s\n", json);
52         free(json);
53         goto out;
54     }
55 
56     if (libxl_device_vdispl_add(ctx, domid, &vdispl, 0)) {
57         fprintf(stderr, "libxl_device_vdispl_add failed.\n");
58         rc = ERROR_FAIL; goto out;
59     }
60 
61     rc = 0;
62 
63 out:
64     libxl_device_vdispl_dispose(&vdispl);
65     return rc;
66 }
67 
main_vdispllist(int argc,char ** argv)68 int main_vdispllist(int argc, char **argv)
69 {
70    int opt;
71    int i, j, n;
72    libxl_device_vdispl *vdispls;
73    libxl_vdisplinfo vdisplinfo;
74 
75    SWITCH_FOREACH_OPT(opt, "", NULL, "vdispl-list", 1) {
76        /* No options */
77    }
78 
79    for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
80        uint32_t domid;
81 
82        if (libxl_domain_qualifier_to_domid(ctx, *argv, &domid) < 0) {
83            fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
84            continue;
85        }
86 
87        vdispls = libxl_device_vdispl_list(ctx, domid, &n);
88 
89        if (!vdispls) continue;
90 
91        for (i = 0; i < n; i++) {
92            libxl_vdisplinfo_init(&vdisplinfo);
93            if (libxl_device_vdispl_getinfo(ctx, domid, &vdispls[i],
94                                            &vdisplinfo) == 0) {
95                printf("DevId: %d, BE: %d, handle: %d, state: %d, "
96                       "be-alloc: %d, BE-path: %s, FE-path: %s\n",
97                        vdisplinfo.devid, vdisplinfo.backend_id,
98                        vdisplinfo.frontend_id,
99                        vdisplinfo.state, vdisplinfo.be_alloc,
100                        vdisplinfo.backend, vdisplinfo.frontend);
101 
102                for (j = 0; j < vdisplinfo.num_connectors; j++) {
103                    printf("\tConnector: %d, id: %s, width: %d, height: %d, "
104                           "req-rref: %d, req-evtch: %d, "
105                           "evt-rref: %d, evt-evtch: %d\n",
106                           j, vdisplinfo.connectors[j].id,
107                           vdisplinfo.connectors[j].width,
108                           vdisplinfo.connectors[j].height,
109                           vdisplinfo.connectors[j].req_rref,
110                           vdisplinfo.connectors[j].req_evtch,
111                           vdisplinfo.connectors[j].evt_rref,
112                           vdisplinfo.connectors[j].evt_evtch);
113                }
114            }
115            libxl_vdisplinfo_dispose(&vdisplinfo);
116        }
117        libxl_device_vdispl_list_free(vdispls, n);
118    }
119    return 0;
120 }
121 
main_vdispldetach(int argc,char ** argv)122 int main_vdispldetach(int argc, char **argv)
123 {
124     uint32_t domid, devid;
125     int opt, rc;
126     libxl_device_vdispl vdispl;
127 
128     SWITCH_FOREACH_OPT(opt, "", NULL, "vdispl-detach", 2) {
129         /* No options */
130     }
131 
132     domid = find_domain(argv[optind++]);
133     devid = atoi(argv[optind++]);
134 
135     libxl_device_vdispl_init(&vdispl);
136 
137     if (libxl_devid_to_device_vdispl(ctx, domid, devid, &vdispl)) {
138         fprintf(stderr, "Error: Device %d not connected.\n", devid);
139         rc = ERROR_FAIL;
140         goto out;
141     }
142 
143     rc = libxl_device_vdispl_remove(ctx, domid, &vdispl, 0);
144     if (rc) {
145         fprintf(stderr, "libxl_device_vdispl_remove failed.\n");
146         rc = ERROR_FAIL;
147         goto out;
148     }
149 
150     rc = 0;
151 
152 out:
153     libxl_device_vdispl_dispose(&vdispl);
154     return rc;
155 }
156 
157 /*
158  * Local variables:
159  * mode: C
160  * c-basic-offset: 4
161  * indent-tabs-mode: nil
162  * End:
163  */
164