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         rc = 0;
54         goto out;
55     }
56 
57     if (libxl_device_vdispl_add(ctx, domid, &vdispl, 0)) {
58         fprintf(stderr, "libxl_device_vdispl_add failed.\n");
59         rc = ERROR_FAIL; goto out;
60     }
61 
62     rc = 0;
63 
64 out:
65     libxl_device_vdispl_dispose(&vdispl);
66     return rc;
67 }
68 
main_vdispllist(int argc,char ** argv)69 int main_vdispllist(int argc, char **argv)
70 {
71    int opt;
72    int i, j, n;
73    libxl_device_vdispl *vdispls;
74    libxl_vdisplinfo vdisplinfo;
75 
76    SWITCH_FOREACH_OPT(opt, "", NULL, "vdispl-list", 1) {
77        /* No options */
78    }
79 
80    for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
81        uint32_t domid;
82 
83        if (libxl_domain_qualifier_to_domid(ctx, *argv, &domid) < 0) {
84            fprintf(stderr, "%s is an invalid domain identifier\n", *argv);
85            continue;
86        }
87 
88        vdispls = libxl_device_vdispl_list(ctx, domid, &n);
89 
90        if (!vdispls) continue;
91 
92        for (i = 0; i < n; i++) {
93            libxl_vdisplinfo_init(&vdisplinfo);
94            if (libxl_device_vdispl_getinfo(ctx, domid, &vdispls[i],
95                                            &vdisplinfo) == 0) {
96                printf("DevId: %d, BE: %d, handle: %d, state: %d, "
97                       "be-alloc: %d, BE-path: %s, FE-path: %s\n",
98                        vdisplinfo.devid, vdisplinfo.backend_id,
99                        vdisplinfo.frontend_id,
100                        vdisplinfo.state, vdisplinfo.be_alloc,
101                        vdisplinfo.backend, vdisplinfo.frontend);
102 
103                for (j = 0; j < vdisplinfo.num_connectors; j++) {
104                    printf("\tConnector: %d, id: %s, width: %d, height: %d, "
105                           "req-rref: %d, req-evtch: %d, "
106                           "evt-rref: %d, evt-evtch: %d\n",
107                           j, vdisplinfo.connectors[j].unique_id,
108                           vdisplinfo.connectors[j].width,
109                           vdisplinfo.connectors[j].height,
110                           vdisplinfo.connectors[j].req_rref,
111                           vdisplinfo.connectors[j].req_evtch,
112                           vdisplinfo.connectors[j].evt_rref,
113                           vdisplinfo.connectors[j].evt_evtch);
114                }
115            }
116            libxl_vdisplinfo_dispose(&vdisplinfo);
117        }
118        libxl_device_vdispl_list_free(vdispls, n);
119    }
120    return 0;
121 }
122 
main_vdispldetach(int argc,char ** argv)123 int main_vdispldetach(int argc, char **argv)
124 {
125     uint32_t domid, devid;
126     int opt, rc;
127     libxl_device_vdispl vdispl;
128 
129     SWITCH_FOREACH_OPT(opt, "", NULL, "vdispl-detach", 2) {
130         /* No options */
131     }
132 
133     domid = find_domain(argv[optind++]);
134     devid = atoi(argv[optind++]);
135 
136     libxl_device_vdispl_init(&vdispl);
137 
138     if (libxl_devid_to_device_vdispl(ctx, domid, devid, &vdispl)) {
139         fprintf(stderr, "Error: Device %d not connected.\n", devid);
140         rc = ERROR_FAIL;
141         goto out;
142     }
143 
144     rc = libxl_device_vdispl_remove(ctx, domid, &vdispl, 0);
145     if (rc) {
146         fprintf(stderr, "libxl_device_vdispl_remove failed.\n");
147         rc = ERROR_FAIL;
148         goto out;
149     }
150 
151     rc = 0;
152 
153 out:
154     libxl_device_vdispl_dispose(&vdispl);
155     return rc;
156 }
157 
158 /*
159  * Local variables:
160  * mode: C
161  * c-basic-offset: 4
162  * indent-tabs-mode: nil
163  * End:
164  */
165