-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test_module.c
More file actions
249 lines (190 loc) · 6.15 KB
/
Copy pathcache_test_module.c
File metadata and controls
249 lines (190 loc) · 6.15 KB
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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include "cache_structure.h"
#include "hashing.h"
#include "hpl_entry.h"
#include "test_consts.h"
MODULE_LICENSE("GPL");
char module_header[] = "cache_test_module: ";
struct cache *c;
/************/
struct ht_entry {
unsigned char hash[HASH_LEN];
unsigned char packets_num;
// HLIST_HEAD(packets_list);
struct hlist_head packets_list;
struct hlist_node ht_node;
};
struct data_entry {
int cnt;
struct hpl_entry data;
struct rb_node tree_node;
struct hlist_node list_node;
struct ht_entry *same_hash_packets;
unsigned char id;
};
void tree_print(struct rb_root *root) {
struct rb_node *node;
for (node = rb_first(root); node; node = rb_next(node)) {
int i;
struct data_entry *entry;
entry = container_of(node, struct data_entry, tree_node);
printk("%sPayload: ", module_header);
for (i = 0; i < entry->data.size; ++i) {
printk("%x", entry->data.pl[i]);
}
printk(" Freq: %d ", entry->cnt);
printk(" Hash: ");
for (i = 0; i < 16; ++i) {
printk("\\x%x", entry->data.hash[i]);
}
printk(" ID: %d", entry->id);
printk("\n");
}
}
void cache_ht_print(struct cache *c) {
struct ht_entry *same_hash_set;
int i, k;
for (k = 0; k < 1 << CACHE_BITS_NUM; ++k) {
hash_for_each_possible(c->ht, same_hash_set, ht_node, k) {
struct data_entry *same_hash_packet;
printk("%sNext same_hash_set, hash : ", module_header);
for (i = 0; i < 16; ++i) {
printk("\\x%x", same_hash_set->hash[i]);
}
printk(" # packets: %d\n", same_hash_set->packets_num);
hlist_for_each_entry(same_hash_packet,
&(same_hash_set->packets_list),
list_node)
{
printk("%sPayload: ", module_header);
for (i = 0; i < same_hash_packet->data.size; ++i) {
printk("%x", same_hash_packet->data.pl[i]);
}
printk(" Freq: %d ", same_hash_packet->cnt);
printk(" ID: %d", same_hash_packet->id);
printk("\n");
}
}
}
}
/************/
int bytes_check(const unsigned char *msg, int msg_len,
const unsigned char *pl, int pl_len, int step_num)
{
int i;
if (pl_len != msg_len) {
printk("%sStep %d, Wrong lengths\n", module_header, step_num);
return 0;
}
for (i = 0; i < pl_len; ++i) {
if (pl[i] != msg[i]) {
printk("%sStep %d, Wrong symbol %d: %x != %x \n",
module_header, step_num, i, pl[i], msg[i]);
return 0;
}
}
return 1;
}
void test_collisions(void) {
init_cache(c, 256);
int i, pl_len;
unsigned char *_hval, _id, *pl, h1[HASH_LEN], h2[HASH_LEN];
printk("%sTest collisions\n", module_header);
add_to_cache(c, m1, M1_LEN, &_hval, &_id);
add_to_cache(c, m2, M2_LEN, &_hval, &_id);
calc_hash(m1, M1_LEN, h1);
calc_hash(m2, M2_LEN, h2);
for (i = 0; i < HASH_LEN; ++i) {
if (h1[i] != h2[i]) {
printk("%sHashes are different at %d: %x != %x\n",
module_header, i, h1[i], h2[i]);
return;
}
}
get_pl_info(c, h1, 0, &pl, &pl_len);
if (bytes_check(m1, M1_LEN, pl, pl_len, 0)) {
printk("%sStep %d - ok\n", module_header, 0);
}
get_pl_info(c, h2, 1, &pl, &pl_len);
if (bytes_check(m2, M2_LEN, pl, pl_len, 1)) {
printk("%sStep %d - ok\n", module_header, 1);
}
add_to_cache(c, m2, M2_LEN, &_hval, &_id);
add_to_cache(c, m2, M2_LEN, &_hval, &_id);
__cache_del_entry(c);
struct rb_node *node = rb_first(&(c->tree));
struct data_entry *entry;
entry = container_of(node, struct data_entry, tree_node);
if (entry->id != 0) {
printk("%sID haven't been changed!\n", module_header);
} else {
printk("%sStep %d - ok\n", module_header, 2);
}
if (entry->cnt != 4) {
printk("%sPacket freq is wrong!\n", module_header);
} else {
printk("%sStep %d - ok\n", module_header, 3);
}
clean_cache(c);
}
void test_basic_functionality(void) {
init_cache(c, 256);
int i, j, entries_num = 3, msg_len = 5;
unsigned char words[entries_num][msg_len];
unsigned char ch = 'a';
for (i = 0; i < entries_num; ++i) {
for (j = 0; j < msg_len; ++j) {
words[i][j] = ch + i;
}
for (j = 0; j < entries_num - i; ++j) {
unsigned char *_hval, _id;
add_to_cache(c, words[i], msg_len, &_hval, &_id);
}
}
printk("%sTest getting payload from cache:\n", module_header);
for (i = 0; i < entries_num; ++i) {
// printk("%sNext payload: ", module_header);
unsigned char *pl, h[HASH_LEN];
int s;
calc_hash(words[i], msg_len, h);
get_pl_info(c, h, 0, &pl, &s);
if (s != msg_len) {
printk("%sWrong message len at %d word!",
module_header, i);
return;
}
for (j = 0; j < s; ++j) {
if (pl[j] != words[i][j]) {
printk("%sWrong symbol at pos %d, word %d: %x != %x!",
module_header, j, i, pl[j], words[i][j]);
return;
}
}
printk("%sWord %d - ok\n", module_header, i);
}
// printk("%sSize, in bytes: %ld, Hitrate: %d\n",
// module_header, c->curr_size, get_hitrate(c));
if (get_hitrate(c) != 66) {
printk("%sWrong hitrate!", module_header);
} else {
printk("%sStep %d - ok\n", module_header, 0);
}
clean_cache(c);
}
int init_func(void) {
printk("%sStart working with cache\n", module_header);
c = kmalloc(sizeof(struct cache), GFP_KERNEL);
alloc_hash_structs();
test_basic_functionality();
test_collisions();
return 0;
}
void exit_func(void) {
kfree(c);
free_hash_structs();
printk("%sStop working with cache\n", module_header);
}
module_init(init_func);
module_exit(exit_func);