1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "internal.h"
#include "testutils.h"
#define LIBVIRT_VIRNETDEVPRIV_H_ALLOW
#ifdef __linux__
# include "virmock.h"
# include "virnetdevpriv.h"
# define VIR_FROM_THIS VIR_FROM_NONE
struct testVirNetDevGetLinkInfoData {
const char *ifname; /* ifname to get info on */
virNetDevIfState state; /* expected state */
unsigned int speed; /* expected speed */
};
static int
testVirNetDevGetLinkInfo(const void *opaque)
{
const struct testVirNetDevGetLinkInfoData *data = opaque;
virNetDevIfLink lnk;
if (virNetDevGetLinkInfo(data->ifname, &lnk) < 0)
return -1;
if (lnk.state != data->state) {
fprintf(stderr,
"Fetched link state (%s) doesn't match the expected one (%s)",
virNetDevIfStateTypeToString(lnk.state),
virNetDevIfStateTypeToString(data->state));
return -1;
}
if (lnk.speed != data->speed) {
fprintf(stderr,
"Fetched link speed (%u) doesn't match the expected one (%u)",
lnk.speed, data->speed);
return -1;
}
return 0;
}
# if defined(WITH_LIBNL)
int
(*real_virNetDevSendVfSetLinkRequest)(const char *ifname,
int vfInfoType,
const void *payload,
const size_t payloadLen);
int
(*real_virNetDevSetVfMac)(const char *ifname,
int vf,
const virMacAddr *macaddr,
bool *allowRetry);
int
(*real_virNetDevSetVfVlan)(const char *ifname,
int vf,
const int *vlanid);
static void
init_syms(void)
{
VIR_MOCK_REAL_INIT(virNetDevSendVfSetLinkRequest);
VIR_MOCK_REAL_INIT(virNetDevSetVfMac);
VIR_MOCK_REAL_INIT(virNetDevSetVfVlan);
}
int
virNetDevSetVfMac(const char *ifname,
int vf,
const virMacAddr *macaddr,
bool *allowRetry)
{
init_syms();
if (STREQ_NULLABLE(ifname, "fakeiface-macerror")) {
return -EBUSY;
} else if (STREQ_NULLABLE(ifname, "fakeiface-altmacerror")) {
return -EINVAL;
} else if (STREQ_NULLABLE(ifname, "fakeiface-macerror-novlanerror")) {
return -EAGAIN;
} else if (STREQ_NULLABLE(ifname, "fakeiface-macerror-vlanerror")) {
return -ENODEV;
} else if (STREQ_NULLABLE(ifname, "fakeiface-nomacerror-vlanerror")) {
return 0;
} else if (STREQ_NULLABLE(ifname, "fakeiface-nomacerror-novlanerror")) {
return 0;
}
return real_virNetDevSetVfMac(ifname, vf, macaddr, allowRetry);
}
int
virNetDevSetVfVlan(const char *ifname,
int vf,
const int *vlanid)
{
init_syms();
if (STREQ_NULLABLE(ifname, "fakeiface-macerror-vlanerror")) {
return -EPERM;
} else if (STREQ_NULLABLE(ifname, "fakeiface-nomacerror-vlanerror")) {
return -EPERM;
} else if (STREQ_NULLABLE(ifname, "fakeiface-macerror-novlanerror")) {
return 0;
} else if (STREQ_NULLABLE(ifname, "fakeiface-nomacerror-novlanerror")) {
return 0;
}
return real_virNetDevSetVfVlan(ifname, vf, vlanid);
}
int
virNetDevSendVfSetLinkRequest(const char *ifname,
int vfInfoType,
const void *payload,
const size_t payloadLen)
{
init_syms();
if (STREQ_NULLABLE(ifname, "fakeiface-eperm")) {
return -EPERM;
} else if (STREQ_NULLABLE(ifname, "fakeiface-eagain")) {
return -EAGAIN;
} else if (STREQ_NULLABLE(ifname, "fakeiface-einval")) {
return -EINVAL;
} else if (STREQ_NULLABLE(ifname, "fakeiface-ok")) {
return 0;
}
return real_virNetDevSendVfSetLinkRequest(ifname, vfInfoType, payload, payloadLen);
}
static int
testVirNetDevSetVfMac(const void *opaque G_GNUC_UNUSED)
{
struct testCase {
const char *ifname;
const int vf_num;
const virMacAddr macaddr;
bool allow_retry;
const int rc;
};
size_t i = 0;
int rc = 0;
struct testCase testCases[] = {
{ .ifname = "fakeiface-ok", .vf_num = 1,
.macaddr = { .addr = { 0, 0, 0, 0, 0, 0 } }, .allow_retry = false, .rc = 0 },
{ .ifname = "fakeiface-ok", .vf_num = 2,
.macaddr = { .addr = { 0, 0, 0, 7, 7, 7 } }, .allow_retry = false, .rc = 0 },
{ .ifname = "fakeiface-ok", .vf_num = 3,
.macaddr = { .addr = { 0, 0, 0, 0, 0, 0 } }, .allow_retry = true, .rc = 0 },
{ .ifname = "fakeiface-ok", .vf_num = 4,
.macaddr = { .addr = { 0, 0, 0, 7, 7, 7 } }, .allow_retry = true, .rc = 0 },
{ .ifname = "fakeiface-eperm", .vf_num = 5,
.macaddr = { .addr = { 0, 0, 0, 0, 0, 0 } }, .allow_retry = false, .rc = -EPERM },
{ .ifname = "fakeiface-einval", .vf_num = 6,
.macaddr = { .addr = { 0, 0, 0, 0, 0, 0 } }, .allow_retry = false, .rc = -EINVAL },
{ .ifname = "fakeiface-einval", .vf_num = 7,
.macaddr = { .addr = { 0, 0, 0, 0, 0, 0 } }, .allow_retry = true, .rc = -EINVAL },
{ .ifname = "fakeiface-einval", .vf_num = 8,
.macaddr = { .addr = { 0, 0, 0, 7, 7, 7 } }, .allow_retry = false, .rc = -EINVAL },
{ .ifname = "fakeiface-einval", .vf_num = 9,
.macaddr = { .addr = { 0, 0, 0, 7, 7, 7 } }, .allow_retry = true, .rc = -EINVAL },
};
for (i = 0; i < G_N_ELEMENTS(testCases); ++i) {
rc = virNetDevSetVfMac(testCases[i].ifname, testCases[i].vf_num,
&testCases[i].macaddr, &testCases[i].allow_retry);
if (rc != testCases[i].rc) {
return -1;
}
}
return 0;
}
static int
testVirNetDevSetVfMissingMac(const void *opaque G_GNUC_UNUSED)
{
bool allowRetry = false;
/* NULL MAC pointer. */
if (virNetDevSetVfMac("fakeiface-ok", 1, NULL, &allowRetry) != -EINVAL) {
return -1;
}
allowRetry = true;
if (virNetDevSetVfMac("fakeiface-ok", 1, NULL, &allowRetry) != -EINVAL) {
return -1;
}
return 0;
}
static int
testVirNetDevSetVfVlan(const void *opaque G_GNUC_UNUSED)
{
struct testCase {
const char *ifname;
const int vf_num;
const int vlan_id;
const int rc;
};
struct nullVlanTestCase {
const char *ifname;
const int vf_num;
const int rc;
};
size_t i = 0;
int rc = 0;
const struct testCase testCases[] = {
/* VLAN ID is out of range of valid values (0-4095). */
{ .ifname = "enxdeadbeefcafe", .vf_num = 1, .vlan_id = 4096, .rc = -ERANGE },
{ .ifname = "enxdeadbeefcafe", .vf_num = 1, .vlan_id = -1, .rc = -ERANGE },
{ .ifname = "fakeiface-eperm", .vf_num = 1, .vlan_id = 0, .rc = -EPERM },
{ .ifname = "fakeiface-eagain", .vf_num = 1, .vlan_id = 0, .rc = -EAGAIN },
/* Successful requests with vlan id 0 need to have a zero return code. */
{ .ifname = "fakeiface-ok", .vf_num = 1, .vlan_id = 0, .rc = 0 },
/* Requests with a non-zero VLAN ID that result in an EPERM need to result in failures.
* failures. */
{ .ifname = "fakeiface-eperm", .vf_num = 1, .vlan_id = 42, .rc = -EPERM },
/* Requests with a non-zero VLAN ID that result in some other errors need to result in
* failures. */
{ .ifname = "fakeiface-eagain", .vf_num = 1, .vlan_id = 42, .rc = -EAGAIN },
/* Successful requests with a non-zero VLAN ID */
{ .ifname = "fakeiface-ok", .vf_num = 1, .vlan_id = 42, .rc = 0 },
};
const struct nullVlanTestCase nullVLANTestCases[] = {
{ .ifname = "fakeiface-eperm", .vf_num = 1, .rc = 0 },
{ .ifname = "fakeiface-eagain", .vf_num = 1, .rc = -EAGAIN },
/* Successful requests with vlan id 0 need to have a zero return code. */
{ .ifname = "fakeiface-ok", .vf_num = 1, .rc = 0 },
};
for (i = 0; i < G_N_ELEMENTS(testCases); ++i) {
rc = virNetDevSetVfVlan(testCases[i].ifname, testCases[i].vf_num, &testCases[i].vlan_id);
if (rc != testCases[i].rc) {
return -1;
}
}
for (i = 0; i < G_N_ELEMENTS(nullVLANTestCases); ++i) {
rc = virNetDevSetVfVlan(nullVLANTestCases[i].ifname, nullVLANTestCases[i].vf_num, NULL);
if (rc != nullVLANTestCases[i].rc) {
return -1;
}
}
return 0;
}
static int
testVirNetDevSetVfConfig(const void *opaque G_GNUC_UNUSED)
{
struct testCase {
const char *ifname;
const int rc;
};
int rc = 0;
size_t i = 0;
/* Nested functions are mocked so dummy values are used. */
const virMacAddr mac = { .addr = { 0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE }};
const int vfNum = 1;
const int vlanid = 0;
bool *allowRetry = NULL;
const struct testCase testCases[] = {
{ .ifname = "fakeiface-macerror", .rc = -EBUSY },
{ .ifname = "fakeiface-altmacerror", .rc = -EINVAL },
{ .ifname = "fakeiface-macerror-novlanerror", .rc = -EAGAIN },
{ .ifname = "fakeiface-macerror-vlanerror", .rc = -ENODEV },
{ .ifname = "fakeiface-nomacerror-novlanerror", .rc = 0 },
};
for (i = 0; i < G_N_ELEMENTS(testCases); ++i) {
rc = virNetDevSetVfConfig(testCases[i].ifname, vfNum, &mac, &vlanid, allowRetry);
if (rc != testCases[i].rc) {
return -1;
}
}
return 0;
}
# endif /* defined(WITH_LIBNL) */
static int
mymain(void)
{
int ret = 0;
# define DO_TEST_LINK(ifname, state, speed) \
do { \
struct testVirNetDevGetLinkInfoData data = {ifname, state, speed}; \
if (virTestRun("Link info: " # ifname, \
testVirNetDevGetLinkInfo, &data) < 0) \
ret = -1; \
} while (0)
DO_TEST_LINK("eth0", VIR_NETDEV_IF_STATE_UP, 1000);
DO_TEST_LINK("lo", VIR_NETDEV_IF_STATE_UNKNOWN, 0);
DO_TEST_LINK("eth0-broken", VIR_NETDEV_IF_STATE_DOWN, 0);
# if defined(WITH_LIBNL)
if (virTestRun("Set VF MAC", testVirNetDevSetVfMac, NULL) < 0)
ret = -1;
if (virTestRun("Set VF MAC: missing MAC pointer", testVirNetDevSetVfMissingMac, NULL) < 0)
ret = -1;
if (virTestRun("Set VF VLAN", testVirNetDevSetVfVlan, NULL) < 0)
ret = -1;
if (virTestRun("Set VF Config", testVirNetDevSetVfConfig, NULL) < 0)
ret = -1;
# endif /* defined(WITH_LIBNL) */
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virnetdev"))
#else
int
main(void)
{
return EXIT_AM_SKIP;
}
#endif
|