app/test: check cloning a clone
[dpdk.git] / app / test / test_power_kvm_vm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <string.h>
39
40 #include "test.h"
41
42 #include <rte_power.h>
43 #include <rte_config.h>
44
45 #define TEST_POWER_VM_LCORE_ID            0U
46 #define TEST_POWER_VM_LCORE_OUT_OF_BOUNDS (RTE_MAX_LCORE+1)
47 #define TEST_POWER_VM_LCORE_INVALID       1U
48
49 static int
50 test_power_kvm_vm(void)
51 {
52         int ret;
53         enum power_management_env env;
54
55         ret = rte_power_set_env(PM_ENV_KVM_VM);
56         if (ret != 0) {
57                 printf("Failed on setting environment to PM_ENV_KVM_VM\n");
58                 return -1;
59         }
60
61         /* Test environment configuration */
62         env = rte_power_get_env();
63         if (env != PM_ENV_KVM_VM) {
64                 printf("Unexpectedly got a Power Management environment other than "
65                                 "KVM VM\n");
66                 rte_power_unset_env();
67                 return -1;
68         }
69
70         /* verify that function pointers are not NULL */
71         if (rte_power_freqs == NULL) {
72                 printf("rte_power_freqs should not be NULL, environment has not been "
73                                 "initialised\n");
74                 return -1;
75         }
76         if (rte_power_get_freq == NULL) {
77                 printf("rte_power_get_freq should not be NULL, environment has not "
78                                 "been initialised\n");
79                 return -1;
80         }
81         if (rte_power_set_freq == NULL) {
82                 printf("rte_power_set_freq should not be NULL, environment has not "
83                                 "been initialised\n");
84                 return -1;
85         }
86         if (rte_power_freq_up == NULL) {
87                 printf("rte_power_freq_up should not be NULL, environment has not "
88                                 "been initialised\n");
89                 return -1;
90         }
91         if (rte_power_freq_down == NULL) {
92                 printf("rte_power_freq_down should not be NULL, environment has not "
93                                 "been initialised\n");
94                 return -1;
95         }
96         if (rte_power_freq_max == NULL) {
97                 printf("rte_power_freq_max should not be NULL, environment has not "
98                                 "been initialised\n");
99                 return -1;
100         }
101         if (rte_power_freq_min == NULL) {
102                 printf("rte_power_freq_min should not be NULL, environment has not "
103                                 "been initialised\n");
104                 return -1;
105         }
106         /* Test initialisation of an out of bounds lcore */
107         ret = rte_power_init(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
108         if (ret != -1) {
109                 printf("rte_power_init unexpectedly succeeded on an invalid lcore %u\n",
110                                 TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
111                 rte_power_unset_env();
112                 return -1;
113         }
114
115         /* Test initialisation of a valid lcore */
116         ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
117         if (ret < 0) {
118                 printf("Cannot initialise power management for lcore %u, this "
119                                 "may occur if environment is not configured "
120                                 "correctly(KVM VM) or operating in another valid "
121                                 "Power management environment\n", TEST_POWER_VM_LCORE_ID);
122                 rte_power_unset_env();
123                 return -1;
124         }
125
126         /* Test initialisation of previously initialised lcore */
127         ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
128         if (ret == 0) {
129                 printf("rte_power_init unexpectedly succeeded on calling init twice on"
130                                 " lcore %u\n", TEST_POWER_VM_LCORE_ID);
131                 goto fail_all;
132         }
133
134         /* Test frequency up of invalid lcore */
135         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
136         if (ret == 1) {
137                 printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
138                                 TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
139                 goto fail_all;
140         }
141
142         /* Test frequency down of invalid lcore */
143         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
144         if (ret == 1) {
145                 printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
146                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
147                 goto fail_all;
148         }
149
150         /* Test frequency min of invalid lcore */
151         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
152         if (ret == 1) {
153                 printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
154                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
155                 goto fail_all;
156         }
157
158         /* Test frequency max of invalid lcore */
159         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
160         if (ret == 1) {
161                 printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
162                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
163                 goto fail_all;
164         }
165
166         /* Test frequency up of valid but uninitialised lcore */
167         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_INVALID);
168         if (ret == 1) {
169                 printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
170                                 TEST_POWER_VM_LCORE_INVALID);
171                 goto fail_all;
172         }
173
174         /* Test frequency down of valid but uninitialised lcore */
175         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_INVALID);
176         if (ret == 1) {
177                 printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
178                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
179                 goto fail_all;
180         }
181
182         /* Test frequency min of valid but uninitialised lcore */
183         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_INVALID);
184         if (ret == 1) {
185                 printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
186                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
187                 goto fail_all;
188         }
189
190         /* Test frequency max of valid but uninitialised lcore */
191         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_INVALID);
192         if (ret == 1) {
193                 printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
194                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
195                 goto fail_all;
196         }
197
198         /* Test frequency up of valid lcore */
199         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
200         if (ret != 1) {
201                 printf("rte_power_freq_up unexpectedly failed on valid lcore %u\n",
202                                 TEST_POWER_VM_LCORE_ID);
203                 goto fail_all;
204         }
205
206         /* Test frequency down of valid lcore */
207         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
208         if (ret != 1) {
209                 printf("rte_power_freq_down unexpectedly failed on valid lcore "
210                                 "%u\n", TEST_POWER_VM_LCORE_ID);
211                 goto fail_all;
212         }
213
214         /* Test frequency min of valid lcore */
215         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
216         if (ret != 1) {
217                 printf("rte_power_freq_min unexpectedly failed on valid lcore "
218                                 "%u\n", TEST_POWER_VM_LCORE_ID);
219                 goto fail_all;
220         }
221
222         /* Test frequency max of valid lcore */
223         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
224         if (ret != 1) {
225                 printf("rte_power_freq_max unexpectedly failed on valid lcore "
226                                 "%u\n", TEST_POWER_VM_LCORE_ID);
227                 goto fail_all;
228         }
229
230         /* Test unsupported rte_power_freqs */
231         ret = rte_power_freqs(TEST_POWER_VM_LCORE_ID, NULL, 0);
232         if (ret != -ENOTSUP) {
233                 printf("rte_power_freqs did not return the expected -ENOTSUP(%d) but "
234                                 "returned %d\n", -ENOTSUP, ret);
235                 goto fail_all;
236         }
237
238         /* Test unsupported rte_power_get_freq */
239         ret = rte_power_get_freq(TEST_POWER_VM_LCORE_ID);
240         if (ret != -ENOTSUP) {
241                 printf("rte_power_get_freq did not return the expected -ENOTSUP(%d) but"
242                                 " returned %d for lcore %u\n",
243                                 -ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
244                 goto fail_all;
245         }
246
247         /* Test unsupported rte_power_set_freq */
248         ret = rte_power_set_freq(TEST_POWER_VM_LCORE_ID, 0);
249         if (ret != -ENOTSUP) {
250                 printf("rte_power_set_freq did not return the expected -ENOTSUP(%d) but"
251                                 " returned %d for lcore %u\n",
252                                 -ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
253                 goto fail_all;
254         }
255
256         /* Test removing of an lcore */
257         ret = rte_power_exit(TEST_POWER_VM_LCORE_ID);
258         if (ret != 0) {
259                 printf("rte_power_exit unexpectedly failed on valid lcore %u,"
260                                 "please ensure that the environment has been configured "
261                                 "correctly\n", TEST_POWER_VM_LCORE_ID);
262                 goto fail_all;
263         }
264
265         /* Test frequency up of previously removed lcore */
266         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
267         if (ret == 0) {
268                 printf("rte_power_freq_up unexpectedly succeeded on a removed "
269                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
270                 return -1;
271         }
272
273         /* Test frequency down of previously removed lcore */
274         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
275         if (ret == 0) {
276                 printf("rte_power_freq_down unexpectedly succeeded on a removed "
277                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
278                 return -1;
279         }
280
281         /* Test frequency min of previously removed lcore */
282         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
283         if (ret == 0) {
284                 printf("rte_power_freq_min unexpectedly succeeded on a removed "
285                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
286                 return -1;
287         }
288
289         /* Test frequency max of previously removed lcore */
290         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
291         if (ret == 0) {
292                 printf("rte_power_freq_max unexpectedly succeeded on a removed "
293                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
294                 return -1;
295         }
296         rte_power_unset_env();
297         return 0;
298 fail_all:
299         rte_power_exit(TEST_POWER_VM_LCORE_ID);
300         rte_power_unset_env();
301         return -1;
302 }
303
304 static struct test_command power_kvm_vm_cmd = {
305         .command = "power_kvm_vm_autotest",
306         .callback = test_power_kvm_vm,
307 };
308 REGISTER_TEST_COMMAND(power_kvm_vm_cmd);