New upstream version 18.02
[deb_dpdk.git] / test / test / test_power_kvm_vm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include <string.h>
10
11 #include "test.h"
12
13 #ifndef RTE_LIBRTE_POWER
14
15 static int
16 test_power_kvm_vm(void)
17 {
18         printf("Power management library not supported, skipping test\n");
19         return TEST_SKIPPED;
20 }
21
22 #else
23 #include <rte_power.h>
24
25 #define TEST_POWER_VM_LCORE_ID            0U
26 #define TEST_POWER_VM_LCORE_OUT_OF_BOUNDS (RTE_MAX_LCORE+1)
27 #define TEST_POWER_VM_LCORE_INVALID       1U
28
29 static int
30 test_power_kvm_vm(void)
31 {
32         int ret;
33         enum power_management_env env;
34
35         ret = rte_power_set_env(PM_ENV_KVM_VM);
36         if (ret != 0) {
37                 printf("Failed on setting environment to PM_ENV_KVM_VM\n");
38                 return -1;
39         }
40
41         /* Test environment configuration */
42         env = rte_power_get_env();
43         if (env != PM_ENV_KVM_VM) {
44                 printf("Unexpectedly got a Power Management environment other than "
45                                 "KVM VM\n");
46                 rte_power_unset_env();
47                 return -1;
48         }
49
50         /* verify that function pointers are not NULL */
51         if (rte_power_freqs == NULL) {
52                 printf("rte_power_freqs should not be NULL, environment has not been "
53                                 "initialised\n");
54                 return -1;
55         }
56         if (rte_power_get_freq == NULL) {
57                 printf("rte_power_get_freq should not be NULL, environment has not "
58                                 "been initialised\n");
59                 return -1;
60         }
61         if (rte_power_set_freq == NULL) {
62                 printf("rte_power_set_freq should not be NULL, environment has not "
63                                 "been initialised\n");
64                 return -1;
65         }
66         if (rte_power_freq_up == NULL) {
67                 printf("rte_power_freq_up should not be NULL, environment has not "
68                                 "been initialised\n");
69                 return -1;
70         }
71         if (rte_power_freq_down == NULL) {
72                 printf("rte_power_freq_down should not be NULL, environment has not "
73                                 "been initialised\n");
74                 return -1;
75         }
76         if (rte_power_freq_max == NULL) {
77                 printf("rte_power_freq_max should not be NULL, environment has not "
78                                 "been initialised\n");
79                 return -1;
80         }
81         if (rte_power_freq_min == NULL) {
82                 printf("rte_power_freq_min should not be NULL, environment has not "
83                                 "been initialised\n");
84                 return -1;
85         }
86         /* Test initialisation of an out of bounds lcore */
87         ret = rte_power_init(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
88         if (ret != -1) {
89                 printf("rte_power_init unexpectedly succeeded on an invalid lcore %u\n",
90                                 TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
91                 rte_power_unset_env();
92                 return -1;
93         }
94
95         /* Test initialisation of a valid lcore */
96         ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
97         if (ret < 0) {
98                 printf("Cannot initialise power management for lcore %u, this "
99                                 "may occur if environment is not configured "
100                                 "correctly(KVM VM) or operating in another valid "
101                                 "Power management environment\n", TEST_POWER_VM_LCORE_ID);
102                 rte_power_unset_env();
103                 return -1;
104         }
105
106         /* Test initialisation of previously initialised lcore */
107         ret = rte_power_init(TEST_POWER_VM_LCORE_ID);
108         if (ret == 0) {
109                 printf("rte_power_init unexpectedly succeeded on calling init twice on"
110                                 " lcore %u\n", TEST_POWER_VM_LCORE_ID);
111                 goto fail_all;
112         }
113
114         /* Test frequency up of invalid lcore */
115         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
116         if (ret == 1) {
117                 printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
118                                 TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
119                 goto fail_all;
120         }
121
122         /* Test frequency down of invalid lcore */
123         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
124         if (ret == 1) {
125                 printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
126                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
127                 goto fail_all;
128         }
129
130         /* Test frequency min of invalid lcore */
131         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
132         if (ret == 1) {
133                 printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
134                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
135                 goto fail_all;
136         }
137
138         /* Test frequency max of invalid lcore */
139         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
140         if (ret == 1) {
141                 printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
142                                 "%u\n", TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
143                 goto fail_all;
144         }
145
146         /* Test frequency up of valid but uninitialised lcore */
147         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_INVALID);
148         if (ret == 1) {
149                 printf("rte_power_freq_up unexpectedly succeeded on invalid lcore %u\n",
150                                 TEST_POWER_VM_LCORE_INVALID);
151                 goto fail_all;
152         }
153
154         /* Test frequency down of valid but uninitialised lcore */
155         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_INVALID);
156         if (ret == 1) {
157                 printf("rte_power_freq_down unexpectedly succeeded on invalid lcore "
158                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
159                 goto fail_all;
160         }
161
162         /* Test frequency min of valid but uninitialised lcore */
163         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_INVALID);
164         if (ret == 1) {
165                 printf("rte_power_freq_min unexpectedly succeeded on invalid lcore "
166                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
167                 goto fail_all;
168         }
169
170         /* Test frequency max of valid but uninitialised lcore */
171         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_INVALID);
172         if (ret == 1) {
173                 printf("rte_power_freq_max unexpectedly succeeded on invalid lcore "
174                                 "%u\n", TEST_POWER_VM_LCORE_INVALID);
175                 goto fail_all;
176         }
177
178         /* Test frequency up of valid lcore */
179         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
180         if (ret != 1) {
181                 printf("rte_power_freq_up unexpectedly failed on valid lcore %u\n",
182                                 TEST_POWER_VM_LCORE_ID);
183                 goto fail_all;
184         }
185
186         /* Test frequency down of valid lcore */
187         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
188         if (ret != 1) {
189                 printf("rte_power_freq_down unexpectedly failed on valid lcore "
190                                 "%u\n", TEST_POWER_VM_LCORE_ID);
191                 goto fail_all;
192         }
193
194         /* Test frequency min of valid lcore */
195         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
196         if (ret != 1) {
197                 printf("rte_power_freq_min unexpectedly failed on valid lcore "
198                                 "%u\n", TEST_POWER_VM_LCORE_ID);
199                 goto fail_all;
200         }
201
202         /* Test frequency max of valid lcore */
203         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
204         if (ret != 1) {
205                 printf("rte_power_freq_max unexpectedly failed on valid lcore "
206                                 "%u\n", TEST_POWER_VM_LCORE_ID);
207                 goto fail_all;
208         }
209
210         /* Test unsupported rte_power_freqs */
211         ret = rte_power_freqs(TEST_POWER_VM_LCORE_ID, NULL, 0);
212         if (ret != -ENOTSUP) {
213                 printf("rte_power_freqs did not return the expected -ENOTSUP(%d) but "
214                                 "returned %d\n", -ENOTSUP, ret);
215                 goto fail_all;
216         }
217
218         /* Test unsupported rte_power_get_freq */
219         ret = rte_power_get_freq(TEST_POWER_VM_LCORE_ID);
220         if (ret != -ENOTSUP) {
221                 printf("rte_power_get_freq did not return the expected -ENOTSUP(%d) but"
222                                 " returned %d for lcore %u\n",
223                                 -ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
224                 goto fail_all;
225         }
226
227         /* Test unsupported rte_power_set_freq */
228         ret = rte_power_set_freq(TEST_POWER_VM_LCORE_ID, 0);
229         if (ret != -ENOTSUP) {
230                 printf("rte_power_set_freq did not return the expected -ENOTSUP(%d) but"
231                                 " returned %d for lcore %u\n",
232                                 -ENOTSUP, ret, TEST_POWER_VM_LCORE_ID);
233                 goto fail_all;
234         }
235
236         /* Test removing of an lcore */
237         ret = rte_power_exit(TEST_POWER_VM_LCORE_ID);
238         if (ret != 0) {
239                 printf("rte_power_exit unexpectedly failed on valid lcore %u,"
240                                 "please ensure that the environment has been configured "
241                                 "correctly\n", TEST_POWER_VM_LCORE_ID);
242                 goto fail_all;
243         }
244
245         /* Test frequency up of previously removed lcore */
246         ret = rte_power_freq_up(TEST_POWER_VM_LCORE_ID);
247         if (ret == 0) {
248                 printf("rte_power_freq_up unexpectedly succeeded on a removed "
249                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
250                 return -1;
251         }
252
253         /* Test frequency down of previously removed lcore */
254         ret = rte_power_freq_down(TEST_POWER_VM_LCORE_ID);
255         if (ret == 0) {
256                 printf("rte_power_freq_down unexpectedly succeeded on a removed "
257                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
258                 return -1;
259         }
260
261         /* Test frequency min of previously removed lcore */
262         ret = rte_power_freq_min(TEST_POWER_VM_LCORE_ID);
263         if (ret == 0) {
264                 printf("rte_power_freq_min unexpectedly succeeded on a removed "
265                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
266                 return -1;
267         }
268
269         /* Test frequency max of previously removed lcore */
270         ret = rte_power_freq_max(TEST_POWER_VM_LCORE_ID);
271         if (ret == 0) {
272                 printf("rte_power_freq_max unexpectedly succeeded on a removed "
273                                 "lcore %u\n", TEST_POWER_VM_LCORE_ID);
274                 return -1;
275         }
276         rte_power_unset_env();
277         return 0;
278 fail_all:
279         rte_power_exit(TEST_POWER_VM_LCORE_ID);
280         rte_power_unset_env();
281         return -1;
282 }
283 #endif
284
285 REGISTER_TEST_COMMAND(power_kvm_vm_autotest, test_power_kvm_vm);