summaryrefslogtreecommitdiff
path: root/net/mac80211/tests/s1g_tim.c
blob: 642fa4ece89ff1ba41aa90cce954f299e8a83421 (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * KUnit tests for S1G TIM PVB decoding. This test suite covers
 * IEEE80211-2024 Annex L figures 8, 9, 10, 12, 13, 14. ADE mode
 * is not covered as it is an optional encoding format and is not
 * currently supported by mac80211.
 *
 * Copyright (C) 2025 Morse Micro
 */
#include <linux/ieee80211.h>
#include <kunit/test.h>
#include <kunit/test-bug.h>

#define MAX_AID 128

#define BC(enc_mode, inverse, blk_off)                          \
	((((blk_off) & 0x1f) << 3) | ((inverse) ? BIT(2) : 0) | \
	 ((enc_mode) & 0x3))

static void byte_to_bitstr(u8 v, char *out)
{
	for (int b = 7; b >= 0; b--)
		*out++ = (v & BIT(b)) ? '1' : '0';
	*out = '\0';
}

static void dump_tim_bits(struct kunit *test,
			  const struct ieee80211_tim_ie *tim, u8 tim_len)
{
	const u8 *ptr = tim->virtual_map;
	const u8 *end = (const u8 *)tim + tim_len;
	unsigned int oct = 1;
	unsigned int blk = 0;
	char bits[9];

	while (ptr < end) {
		u8 ctrl = *ptr++;
		u8 mode = ctrl & 0x03;
		bool inverse = ctrl & BIT(2);
		u8 blk_off = ctrl >> 3;

		kunit_info(
			test, "Block %u (ENC=%s, blk_off=%u, inverse=%u)", blk,
			(mode == IEEE80211_S1G_TIM_ENC_MODE_BLOCK)  ? "BLOCK" :
			(mode == IEEE80211_S1G_TIM_ENC_MODE_SINGLE) ? "SINGLE" :
								      "OLB",
			blk_off, inverse);

		byte_to_bitstr(ctrl, bits);
		kunit_info(test, "  octet %2u (ctrl)    : %s (0x%02x)", oct,
			   bits, ctrl);
		++oct;

		switch (mode) {
		case IEEE80211_S1G_TIM_ENC_MODE_BLOCK: {
			u8 blkmap = *ptr++;

			byte_to_bitstr(blkmap, bits);
			kunit_info(test, "  octet %2u (blk-map) : %s (0x%02x)",
				   oct, bits, blkmap);
			++oct;

			for (u8 sb = 0; sb < 8; sb++) {
				if (!(blkmap & BIT(sb)))
					continue;
				u8 sub = *ptr++;

				byte_to_bitstr(sub, bits);
				kunit_info(
					test,
					"  octet %2u (SB %2u)   : %s (0x%02x)",
					oct, sb, bits, sub);
				++oct;
			}
			break;
		}
		case IEEE80211_S1G_TIM_ENC_MODE_SINGLE: {
			u8 single = *ptr++;

			byte_to_bitstr(single, bits);
			kunit_info(test, "  octet %2u (single)  : %s (0x%02x)",
				   oct, bits, single);
			++oct;
			break;
		}
		case IEEE80211_S1G_TIM_ENC_MODE_OLB: {
			u8 len = *ptr++;

			byte_to_bitstr(len, bits);
			kunit_info(test, "  octet %2u (len=%2u)  : %s (0x%02x)",
				   oct, len, bits, len);
			++oct;

			for (u8 i = 0; i < len && ptr < end; i++) {
				u8 sub = *ptr++;

				byte_to_bitstr(sub, bits);
				kunit_info(
					test,
					"  octet %2u (SB %2u)   : %s (0x%02x)",
					oct, i, bits, sub);
				++oct;
			}
			break;
		}
		default:
			kunit_info(test, "  ** unknown encoding 0x%x **", mode);
			return;
		}
		blk++;
	}
}

static void tim_push(u8 **p, u8 v)
{
	*(*p)++ = v;
}

static void tim_begin(struct ieee80211_tim_ie *tim, u8 **p)
{
	tim->dtim_count = 0;
	tim->dtim_period = 1;
	tim->bitmap_ctrl = 0;
	*p = tim->virtual_map;
}

static u8 tim_end(struct ieee80211_tim_ie *tim, u8 *tail)
{
	return tail - (u8 *)tim;
}

static void pvb_add_block_bitmap(u8 **p, u8 blk_off, bool inverse, u8 blk_bmap,
				 const u8 *subblocks)
{
	u8 enc = IEEE80211_S1G_TIM_ENC_MODE_BLOCK;
	u8 n = hweight8(blk_bmap);

	tim_push(p, BC(enc, inverse, blk_off));
	tim_push(p, blk_bmap);

	for (u8 i = 0; i < n; i++)
		tim_push(p, subblocks[i]);
}

static void pvb_add_single_aid(u8 **p, u8 blk_off, bool inverse, u8 single6)
{
	u8 enc = IEEE80211_S1G_TIM_ENC_MODE_SINGLE;

	tim_push(p, BC(enc, inverse, blk_off));
	tim_push(p, single6 & GENMASK(5, 0));
}

static void pvb_add_olb(u8 **p, u8 blk_off, bool inverse, const u8 *subblocks,
			u8 len)
{
	u8 enc = IEEE80211_S1G_TIM_ENC_MODE_OLB;

	tim_push(p, BC(enc, inverse, blk_off));
	tim_push(p, len);
	for (u8 i = 0; i < len; i++)
		tim_push(p, subblocks[i]);
}

static void check_all_aids(struct kunit *test,
			   const struct ieee80211_tim_ie *tim, u8 tim_len,
			   const unsigned long *expected)
{
	for (u16 aid = 1; aid <= MAX_AID; aid++) {
		bool want = test_bit(aid, expected);
		bool got = ieee80211_s1g_check_tim(tim, tim_len, aid);

		KUNIT_ASSERT_EQ_MSG(test, got, want,
				    "AID %u mismatch (got=%d want=%d)", aid,
				    got, want);
	}
}

static void fill_bitmap(unsigned long *bm, const u16 *list, size_t n)
{
	size_t i;

	bitmap_zero(bm, MAX_AID + 1);
	for (i = 0; i < n; i++)
		__set_bit(list[i], bm);
}

static void fill_bitmap_inverse(unsigned long *bm, u16 max_aid,
				const u16 *except, size_t n_except)
{
	bitmap_zero(bm, MAX_AID + 1);
	for (u16 aid = 1; aid <= max_aid; aid++)
		__set_bit(aid, bm);

	for (size_t i = 0; i < n_except; i++)
		if (except[i] <= max_aid)
			__clear_bit(except[i], bm);
}

static void s1g_tim_block_test(struct kunit *test)
{
	u8 buf[256] = {};
	struct ieee80211_tim_ie *tim = (void *)buf;
	u8 *p, tim_len;
	static const u8 subblocks[] = {
		0x42, /* SB m=0: AIDs 1,6 */
		0xA0, /* SB m=2: AIDs 21,23 */
	};
	u8 blk_bmap = 0x05; /* bits 0 and 2 set */
	bool inverse = false;
	static const u16 set_list[] = { 1, 6, 21, 23 };
	DECLARE_BITMAP(exp, MAX_AID + 1);

	tim_begin(tim, &p);
	pvb_add_block_bitmap(&p, 0, inverse, blk_bmap, subblocks);
	tim_len = tim_end(tim, p);

	fill_bitmap(exp, set_list, ARRAY_SIZE(set_list));

	dump_tim_bits(test, tim, tim_len);
	check_all_aids(test, tim, tim_len, exp);
}

static void s1g_tim_single_test(struct kunit *test)
{
	u8 buf[256] = {};
	struct ieee80211_tim_ie *tim = (void *)buf;
	u8 *p, tim_len;
	bool inverse = false;
	u8 blk_off = 0;
	u8 single6 = 0x1f; /* 31 */
	static const u16 set_list[] = { 31 };
	DECLARE_BITMAP(exp, MAX_AID + 1);

	tim_begin(tim, &p);
	pvb_add_single_aid(&p, blk_off, inverse, single6);
	tim_len = tim_end(tim, p);

	fill_bitmap(exp, set_list, ARRAY_SIZE(set_list));

	dump_tim_bits(test, tim, tim_len);
	check_all_aids(test, tim, tim_len, exp);
}

static void s1g_tim_olb_test(struct kunit *test)
{
	u8 buf[256] = {};
	struct ieee80211_tim_ie *tim = (void *)buf;
	u8 *p, tim_len;
	bool inverse = false;
	u8 blk_off = 0;
	static const u16 set_list[] = { 1,  6,	13, 15, 17, 22, 29, 31, 33,
					38, 45, 47, 49, 54, 61, 63, 65, 70 };
	static const u8 subblocks[] = { 0x42, 0xA0, 0x42, 0xA0, 0x42,
					0xA0, 0x42, 0xA0, 0x42 };
	u8 len = ARRAY_SIZE(subblocks);
	DECLARE_BITMAP(exp, MAX_AID + 1);

	tim_begin(tim, &p);
	pvb_add_olb(&p, blk_off, inverse, subblocks, len);
	tim_len = tim_end(tim, p);

	fill_bitmap(exp, set_list, ARRAY_SIZE(set_list));

	dump_tim_bits(test, tim, tim_len);
	check_all_aids(test, tim, tim_len, exp);
}

static void s1g_tim_inverse_block_test(struct kunit *test)
{
	u8 buf[256] = {};
	struct ieee80211_tim_ie *tim = (void *)buf;
	u8 *p, tim_len;
	/* Same sub-block content as Figure L-8, but inverse = true */
	static const u8 subblocks[] = {
		0x42, /* SB m=0: AIDs 1,6 */
		0xA0, /* SB m=2: AIDs 21,23 */
	};
	u8 blk_bmap = 0x05;
	bool inverse = true;
	/*  All AIDs except 1,6,21,23 are set */
	static const u16 except[] = { 1, 6, 21, 23 };
	DECLARE_BITMAP(exp, MAX_AID + 1);

	tim_begin(tim, &p);
	pvb_add_block_bitmap(&p, 0, inverse, blk_bmap, subblocks);
	tim_len = tim_end(tim, p);

	fill_bitmap_inverse(exp, 63, except, ARRAY_SIZE(except));

	dump_tim_bits(test, tim, tim_len);
	check_all_aids(test, tim, tim_len, exp);
}

static void s1g_tim_inverse_single_test(struct kunit *test)
{
	u8 buf[256] = {};
	struct ieee80211_tim_ie *tim = (void *)buf;
	u8 *p, tim_len;
	bool inverse = true;
	u8 blk_off = 0;
	u8 single6 = 0x1f; /* 31 */
	/*  All AIDs except 31 are set */
	static const u16 except[] = { 31 };
	DECLARE_BITMAP(exp, MAX_AID + 1);

	tim_begin(tim, &p);
	pvb_add_single_aid(&p, blk_off, inverse, single6);
	tim_len = tim_end(tim, p);

	fill_bitmap_inverse(exp, 63, except, ARRAY_SIZE(except));

	dump_tim_bits(test, tim, tim_len);
	check_all_aids(test, tim, tim_len, exp);
}

static void s1g_tim_inverse_olb_test(struct kunit *test)
{
	u8 buf[256] = {};
	struct ieee80211_tim_ie *tim = (void *)buf;
	u8 *p, tim_len;
	bool inverse = true;
	u8 blk_off = 0, len;
	/*  All AIDs except the list below are set */
	static const u16 except[] = { 1,  6,  13, 15, 17, 22, 29, 31, 33,
				      38, 45, 47, 49, 54, 61, 63, 65, 70 };
	static const u8 subblocks[] = { 0x42, 0xA0, 0x42, 0xA0, 0x42,
					0xA0, 0x42, 0xA0, 0x42 };
	len = ARRAY_SIZE(subblocks);
	DECLARE_BITMAP(exp, MAX_AID + 1);

	tim_begin(tim, &p);
	pvb_add_olb(&p, blk_off, inverse, subblocks, len);
	tim_len = tim_end(tim, p);

	fill_bitmap_inverse(exp, 127, except, ARRAY_SIZE(except));

	dump_tim_bits(test, tim, tim_len);
	check_all_aids(test, tim, tim_len, exp);
}

static struct kunit_case s1g_tim_test_cases[] = {
	KUNIT_CASE(s1g_tim_block_test),
	KUNIT_CASE(s1g_tim_single_test),
	KUNIT_CASE(s1g_tim_olb_test),
	KUNIT_CASE(s1g_tim_inverse_block_test),
	KUNIT_CASE(s1g_tim_inverse_single_test),
	KUNIT_CASE(s1g_tim_inverse_olb_test),
	{}
};

static struct kunit_suite s1g_tim = {
	.name = "mac80211-s1g-tim",
	.test_cases = s1g_tim_test_cases,
};

kunit_test_suite(s1g_tim);