summaryrefslogtreecommitdiff
path: root/net/wireless/tests/chan.c
blob: 65eb18c498deb43cca976ae9ef3f459d1784f1fe (plain)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// SPDX-License-Identifier: GPL-2.0-only
/*
 * KUnit tests for channel helper functions
 *
 * Copyright (C) 2023-2024, 2026 Intel Corporation
 */
#include <net/cfg80211.h>
#include <kunit/test.h>

MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");

static struct ieee80211_channel chan_2ghz_1 = {
	.band = NL80211_BAND_2GHZ,
	.center_freq = 2412,
};

static struct ieee80211_channel chan_6ghz_1 = {
	.band = NL80211_BAND_6GHZ,
	.center_freq = 5955,
};

static struct ieee80211_channel chan_6ghz_5 = {
	.band = NL80211_BAND_6GHZ,
	.center_freq = 5975,
};

static struct ieee80211_channel chan_6ghz_105 = {
	.band = NL80211_BAND_6GHZ,
	.center_freq = 6475,
};

static const struct chandef_compat_case {
	const char *desc;
	/* leave c1 empty for tests for identical */
	struct cfg80211_chan_def c1, c2;
	/* we test both ways around, so c2 should always be the compat one */
	bool compat;
} chandef_compat_cases[] = {
	{
		.desc = "identical non-HT",
		.c2 = {
			.width = NL80211_CHAN_WIDTH_20_NOHT,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955,
		},
		.compat = true,
	},
	{
		.desc = "identical 20 MHz",
		.c2 = {
			.width = NL80211_CHAN_WIDTH_20,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955,
		},
		.compat = true,
	},
	{
		.desc = "identical 40 MHz",
		.c2 = {
			.width = NL80211_CHAN_WIDTH_40,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955 + 10,
		},
		.compat = true,
	},
	{
		.desc = "identical 80 MHz",
		.c2 = {
			.width = NL80211_CHAN_WIDTH_80,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955 + 10 + 20,
		},
		.compat = true,
	},
	{
		.desc = "identical 160 MHz",
		.c2 = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955 + 10 + 20 + 40,
		},
		.compat = true,
	},
	{
		.desc = "identical 320 MHz",
		.c2 = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955 + 10 + 20 + 40 + 80,
		},
		.compat = true,
	},
	{
		.desc = "20 MHz in 320 MHz\n",
		.c1 = {
			.width = NL80211_CHAN_WIDTH_20,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955,
		},
		.c2 = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955 + 10 + 20 + 40 + 80,
		},
		.compat = true,
	},
	{
		.desc = "different 20 MHz",
		.c1 = {
			.width = NL80211_CHAN_WIDTH_20,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955,
		},
		.c2 = {
			.width = NL80211_CHAN_WIDTH_20,
			.chan = &chan_6ghz_5,
			.center_freq1 = 5975,
		},
	},
	{
		.desc = "different primary 320 MHz",
		.c1 = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 110,
		},
		.c2 = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 - 50,
		},
	},
	{
		/* similar to previous test but one has lower BW */
		.desc = "matching primary 160 MHz",
		.c1 = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
		},
		.c2 = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 - 50,
		},
		.compat = true,
	},
	{
		.desc = "matching primary 160 MHz & punctured secondary 160 Mhz",
		.c1 = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
		},
		.c2 = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 - 50,
			.punctured = 0xf,
		},
		.compat = true,
	},
	{
		.desc = "matching primary 160 MHz & punctured matching",
		.c1 = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0xc0,
		},
		.c2 = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 - 50,
			.punctured = 0xc000,
		},
		.compat = true,
	},
	{
		.desc = "matching primary 160 MHz & punctured not matching",
		.c1 = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0x80,
		},
		.c2 = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 - 50,
			.punctured = 0xc000,
		},
	},
};

KUNIT_ARRAY_PARAM_DESC(chandef_compat, chandef_compat_cases, desc)

static void test_chandef_compat(struct kunit *test)
{
	const struct chandef_compat_case *params = test->param_value;
	const struct cfg80211_chan_def *ret, *expect;
	struct cfg80211_chan_def c1 = params->c1;

	/* tests with identical ones */
	if (!params->c1.chan)
		c1 = params->c2;

	KUNIT_EXPECT_EQ(test, cfg80211_chandef_valid(&c1), true);
	KUNIT_EXPECT_EQ(test, cfg80211_chandef_valid(&params->c2), true);

	expect = params->compat ? &params->c2 : NULL;

	ret = cfg80211_chandef_compatible(&c1, &params->c2);
	KUNIT_EXPECT_PTR_EQ(test, ret, expect);

	if (!params->c1.chan)
		expect = &c1;

	ret = cfg80211_chandef_compatible(&params->c2, &c1);
	KUNIT_EXPECT_PTR_EQ(test, ret, expect);
}

static const struct chandef_dbe_case {
	const char *desc;
	struct cfg80211_chan_def c;
	u8 dbe[3];
	bool fails;
	u16 cf1;
} chandef_dbe_cases[] = {
	{
		.desc = "non-HT failure",
		.c = {
			.width = NL80211_CHAN_WIDTH_20_NOHT,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_40,
		.fails = true,
	},
	{
		.desc = "2.4 GHz fails",
		.c = {
			.width = NL80211_CHAN_WIDTH_20,
			.chan = &chan_2ghz_1,
			.center_freq1 = 2412,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_40,
		.fails = true,
	},
	{
		.desc = "DBE narrower",
		.c = {
			.width = NL80211_CHAN_WIDTH_320,
			.chan = &chan_6ghz_1,
			.center_freq1 = 5955 + 10 + 20 + 40 + 80,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_160,
		.fails = true,
	},
	{
		.desc = "DBE to 320-1",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_1,
		.cf1 = 6425,
	},
	{
		.desc = "DBE to 320-2",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_2,
		.cf1 = 6585,
	},
	{
		.desc = "bad disabled subchannel bitmap - not enough in BSS (1)",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0x0001,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_1 |
			  IEEE80211_UHR_DBE_OPER_DIS_SUBCHANNEL_BITMAP_PRES,
		/* DBE disabled subchannel bitmap == 0 */
		.fails = true,
	},
	{
		.desc = "bad disabled subchannel bitmap - too much in BSS (1)",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0x0001,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_1 |
			  IEEE80211_UHR_DBE_OPER_DIS_SUBCHANNEL_BITMAP_PRES,
		/* DBE disabled subchannel bitmap == 0x0300 */
		.dbe[2] = 0x03,
		.fails = true,
	},
	{
		.desc = "bad disabled subchannel bitmap - not enough in BSS (2)",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0x0001,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_2 |
			  IEEE80211_UHR_DBE_OPER_DIS_SUBCHANNEL_BITMAP_PRES,
		/* DBE disabled subchannel bitmap == 0 */
		.fails = true,
	},
	{
		.desc = "bad disabled subchannel bitmap - too much in BSS (2)",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0x0001,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_2 |
			  IEEE80211_UHR_DBE_OPER_DIS_SUBCHANNEL_BITMAP_PRES,
		/* DBE disabled subchannel bitmap == 0x03 */
		.dbe[1] = 0x03,
		.fails = true,
	},
	{
		.desc = "bad disabled subchannel bitmap - bad bitmap",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0x0001,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_1 |
			  IEEE80211_UHR_DBE_OPER_DIS_SUBCHANNEL_BITMAP_PRES,
		/* DBE disabled subchannel bitmap == 0x1100 */
		.dbe[2] = 0x11,
		.fails = true,
	},
	{
		.desc = "good disabled subchannel bitmap (1)",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0x0003,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_1 |
			  IEEE80211_UHR_DBE_OPER_DIS_SUBCHANNEL_BITMAP_PRES,
		/* DBE disabled subchannel bitmap == 0x0300 */
		.dbe[2] = 0x03,
		.cf1 = 6425,
	},
	{
		.desc = "good disabled subchannel bitmap (2)",
		.c = {
			.width = NL80211_CHAN_WIDTH_160,
			.chan = &chan_6ghz_105,
			.center_freq1 = 6475 + 30,
			.punctured = 0x0003,
		},
		.dbe[0] = IEEE80211_UHR_DBE_OPER_BW_320_2 |
			  IEEE80211_UHR_DBE_OPER_DIS_SUBCHANNEL_BITMAP_PRES,
		/* DBE disabled subchannel bitmap == 0x0003 */
		.dbe[1] = 0x03,
		.cf1 = 6585,
	},
};

KUNIT_ARRAY_PARAM_DESC(chandef_dbe, chandef_dbe_cases, desc)

static void test_chandef_dbe(struct kunit *test)
{
	const struct chandef_dbe_case *params = test->param_value;
	struct cfg80211_chan_def c = params->c;
	int ret;

	KUNIT_EXPECT_EQ(test, cfg80211_chandef_valid(&params->c), true);

	ret = cfg80211_chandef_add_dbe(&c, (void *)params->dbe);
	KUNIT_EXPECT_EQ(test, ret != 0, params->fails);

	if (params->fails)
		return;

	KUNIT_EXPECT_EQ(test, c.center_freq1, params->cf1);
}

static struct kunit_case chandef_test_cases[] = {
	KUNIT_CASE_PARAM(test_chandef_compat, chandef_compat_gen_params),
	KUNIT_CASE_PARAM(test_chandef_dbe, chandef_dbe_gen_params),
	{}
};

static struct kunit_suite chandef = {
	.name = "cfg80211-chandef",
	.test_cases = chandef_test_cases,
};

kunit_test_suite(chandef);