summaryrefslogtreecommitdiff
path: root/net/hsr/prp_dup_discard_test.c
blob: e86b7b633ae89cccfa39a6c4816eeabd63a91218 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
#include <kunit/test.h>

#include "hsr_main.h"
#include "hsr_framereg.h"

struct prp_test_data {
	struct hsr_port port;
	struct hsr_port port_rcv;
	struct hsr_frame_info frame;
	struct hsr_node node;
};

static struct prp_test_data *build_prp_test_data(struct kunit *test)
{
	struct prp_test_data *data = kunit_kzalloc(test,
		sizeof(struct prp_test_data), GFP_USER);
	KUNIT_EXPECT_NOT_ERR_OR_NULL(test, data);

	data->frame.node_src = &data->node;
	data->frame.port_rcv = &data->port_rcv;
	data->port_rcv.type = HSR_PT_SLAVE_A;
	data->node.seq_start[HSR_PT_SLAVE_A] = 1;
	data->node.seq_expected[HSR_PT_SLAVE_A] = 1;
	data->node.seq_start[HSR_PT_SLAVE_B] = 1;
	data->node.seq_expected[HSR_PT_SLAVE_B] = 1;
	data->node.seq_out[HSR_PT_MASTER] = 0;
	data->node.time_out[HSR_PT_MASTER] = jiffies;
	data->port.type = HSR_PT_MASTER;

	return data;
}

static void check_prp_counters(struct kunit *test,
			       struct prp_test_data *data,
			       u16 seq_start_a, u16 seq_expected_a,
			       u16 seq_start_b, u16 seq_expected_b)
{
	KUNIT_EXPECT_EQ(test, data->node.seq_start[HSR_PT_SLAVE_A],
			seq_start_a);
	KUNIT_EXPECT_EQ(test, data->node.seq_start[HSR_PT_SLAVE_B],
			seq_start_b);
	KUNIT_EXPECT_EQ(test, data->node.seq_expected[HSR_PT_SLAVE_A],
			seq_expected_a);
	KUNIT_EXPECT_EQ(test, data->node.seq_expected[HSR_PT_SLAVE_B],
			seq_expected_b);
}

static void prp_dup_discard_forward(struct kunit *test)
{
	/* Normal situation, both LANs in sync. Next frame is forwarded */
	struct prp_test_data *data = build_prp_test_data(test);

	data->frame.sequence_nr = 2;
	KUNIT_EXPECT_EQ(test, 0,
			prp_register_frame_out(&data->port, &data->frame));
	KUNIT_EXPECT_EQ(test, data->frame.sequence_nr,
			data->node.seq_out[HSR_PT_MASTER]);
	KUNIT_EXPECT_EQ(test, jiffies, data->node.time_out[HSR_PT_MASTER]);
	check_prp_counters(test, data, data->frame.sequence_nr,
			   data->frame.sequence_nr + 1, 1, 1);
}

static void prp_dup_discard_inside_dropwindow(struct kunit *test)
{
	/* Normal situation, other LAN ahead by one. Frame is dropped */
	struct prp_test_data *data = build_prp_test_data(test);
	unsigned long time = jiffies - 10;

	data->frame.sequence_nr = 1;
	data->node.seq_expected[HSR_PT_SLAVE_B] = 3;
	data->node.seq_out[HSR_PT_MASTER] = 2;
	data->node.time_out[HSR_PT_MASTER] = time;

	KUNIT_EXPECT_EQ(test, 1,
			prp_register_frame_out(&data->port, &data->frame));
	KUNIT_EXPECT_EQ(test, 2, data->node.seq_out[HSR_PT_MASTER]);
	KUNIT_EXPECT_EQ(test, time, data->node.time_out[HSR_PT_MASTER]);
	check_prp_counters(test, data, 2, 2, 2, 3);
}

static void prp_dup_discard_node_timeout(struct kunit *test)
{
	/* Timeout situation, node hasn't sent anything for a while */
	struct prp_test_data *data = build_prp_test_data(test);

	data->frame.sequence_nr = 7;
	data->node.seq_start[HSR_PT_SLAVE_A] = 1234;
	data->node.seq_expected[HSR_PT_SLAVE_A] = 1235;
	data->node.seq_start[HSR_PT_SLAVE_B] = 1234;
	data->node.seq_expected[HSR_PT_SLAVE_B] = 1234;
	data->node.seq_out[HSR_PT_MASTER] = 1234;
	data->node.time_out[HSR_PT_MASTER] =
		jiffies - msecs_to_jiffies(HSR_ENTRY_FORGET_TIME) - 1;

	KUNIT_EXPECT_EQ(test, 0,
			prp_register_frame_out(&data->port, &data->frame));
	KUNIT_EXPECT_EQ(test, data->frame.sequence_nr,
			data->node.seq_out[HSR_PT_MASTER]);
	KUNIT_EXPECT_EQ(test, jiffies, data->node.time_out[HSR_PT_MASTER]);
	check_prp_counters(test, data, data->frame.sequence_nr,
			   data->frame.sequence_nr + 1, 1234, 1234);
}

static void prp_dup_discard_out_of_sequence(struct kunit *test)
{
	/* One frame is received out of sequence on both LANs */
	struct prp_test_data *data = build_prp_test_data(test);

	data->node.seq_start[HSR_PT_SLAVE_A] = 10;
	data->node.seq_expected[HSR_PT_SLAVE_A] = 10;
	data->node.seq_start[HSR_PT_SLAVE_B] = 10;
	data->node.seq_expected[HSR_PT_SLAVE_B] = 10;
	data->node.seq_out[HSR_PT_MASTER] = 9;

	/* 1st old frame, should be accepted */
	data->frame.sequence_nr = 8;
	KUNIT_EXPECT_EQ(test, 0,
			prp_register_frame_out(&data->port, &data->frame));
	KUNIT_EXPECT_EQ(test, data->frame.sequence_nr,
			data->node.seq_out[HSR_PT_MASTER]);
	check_prp_counters(test, data, data->frame.sequence_nr,
			   data->frame.sequence_nr + 1, 10, 10);

	/* 2nd frame should be dropped */
	data->frame.sequence_nr = 8;
	data->port_rcv.type = HSR_PT_SLAVE_B;
	KUNIT_EXPECT_EQ(test, 1,
			prp_register_frame_out(&data->port, &data->frame));
	check_prp_counters(test, data, data->frame.sequence_nr + 1,
			   data->frame.sequence_nr + 1,
			   data->frame.sequence_nr + 1,
			   data->frame.sequence_nr + 1);

	/* Next frame, this is forwarded */
	data->frame.sequence_nr = 10;
	data->port_rcv.type = HSR_PT_SLAVE_A;
	KUNIT_EXPECT_EQ(test, 0,
			prp_register_frame_out(&data->port, &data->frame));
	KUNIT_EXPECT_EQ(test, data->frame.sequence_nr,
			data->node.seq_out[HSR_PT_MASTER]);
	check_prp_counters(test, data, data->frame.sequence_nr,
			   data->frame.sequence_nr + 1, 9, 9);

	/* and next one is dropped */
	data->frame.sequence_nr = 10;
	data->port_rcv.type = HSR_PT_SLAVE_B;
	KUNIT_EXPECT_EQ(test, 1,
			prp_register_frame_out(&data->port, &data->frame));
	check_prp_counters(test, data, data->frame.sequence_nr + 1,
			   data->frame.sequence_nr + 1,
			   data->frame.sequence_nr + 1,
			   data->frame.sequence_nr + 1);
}

static void prp_dup_discard_lan_b_late(struct kunit *test)
{
	/* LAN B is behind */
	struct prp_test_data *data = build_prp_test_data(test);

	data->node.seq_start[HSR_PT_SLAVE_A] = 9;
	data->node.seq_expected[HSR_PT_SLAVE_A] = 9;
	data->node.seq_start[HSR_PT_SLAVE_B] = 9;
	data->node.seq_expected[HSR_PT_SLAVE_B] = 9;
	data->node.seq_out[HSR_PT_MASTER] = 8;

	data->frame.sequence_nr = 9;
	KUNIT_EXPECT_EQ(test, 0,
			prp_register_frame_out(&data->port, &data->frame));
	KUNIT_EXPECT_EQ(test, data->frame.sequence_nr,
			data->node.seq_out[HSR_PT_MASTER]);
	check_prp_counters(test, data, 9, 10, 9, 9);

	data->frame.sequence_nr = 10;
	KUNIT_EXPECT_EQ(test, 0,
			prp_register_frame_out(&data->port, &data->frame));
	KUNIT_EXPECT_EQ(test, data->frame.sequence_nr,
			data->node.seq_out[HSR_PT_MASTER]);
	check_prp_counters(test, data, 9, 11, 9, 9);

	data->frame.sequence_nr = 9;
	data->port_rcv.type = HSR_PT_SLAVE_B;
	KUNIT_EXPECT_EQ(test, 1,
			prp_register_frame_out(&data->port, &data->frame));
	check_prp_counters(test, data, 10, 11, 10, 10);

	data->frame.sequence_nr = 10;
	data->port_rcv.type = HSR_PT_SLAVE_B;
	KUNIT_EXPECT_EQ(test, 1,
			prp_register_frame_out(&data->port, &data->frame));
	check_prp_counters(test, data,  11, 11, 11, 11);
}

static struct kunit_case prp_dup_discard_test_cases[] = {
	KUNIT_CASE(prp_dup_discard_forward),
	KUNIT_CASE(prp_dup_discard_inside_dropwindow),
	KUNIT_CASE(prp_dup_discard_node_timeout),
	KUNIT_CASE(prp_dup_discard_out_of_sequence),
	KUNIT_CASE(prp_dup_discard_lan_b_late),
	{}
};

static struct kunit_suite prp_dup_discard_suite = {
	.name = "prp_duplicate_discard",
	.test_cases = prp_dup_discard_test_cases,
};

kunit_test_suite(prp_dup_discard_suite);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("KUnit tests for PRP duplicate discard");
MODULE_AUTHOR("Jaakko Karrenpalo <jkarrenpalo@gmail.com>");