-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
169 lines (134 loc) · 4.21 KB
/
main.c
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
/*
* aqmprobe - A kernel module to probe Qdiscs for drop statistics
*
* Copyright (C) 2014 Jonas Sæther Markussen <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/module.h>
#include "message_queue.h"
#include "qdisc_probe.h"
#include "file_operations.h"
MODULE_AUTHOR("Jonas Sæther Markussen");
MODULE_DESCRIPTION("Qdisc drop statistics probe");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");
/* Qdisc argument to module */
static char* qdisc = NULL;
module_param(qdisc, charp, 0);
MODULE_PARM_DESC(qdisc, "Qdisc to attach to");
/* Maximum number of concurrent packet events argument to module */
static int maximum_concurrent_events = 60;
module_param(maximum_concurrent_events, int, 0);
MODULE_PARM_DESC(maximum_concurrent_events, "Maximum number of concurrent packet events handled");
/* Maximum buffer size argument to module */
static int buffer_size = 0;
module_param(buffer_size, int, 0);
MODULE_PARM_DESC(buffer_size, "Maximum number of buffered packet event reports");
/* Flush frequency */
static int flush_frequency = 1024;
module_param(flush_frequency, int, 0);
MODULE_PARM_DESC(flush_frequency, "Number of buffered packet event reports before triggering file flush");
/* Report file name */
char* filename = "aqmprobe";
module_param(filename, charp, 0);
MODULE_PARM_DESC(filename, "Filename of the report file");
/* Array of known Qdiscs and their entry points */
static struct { const char* qdisc; const char* entry; } known_qdiscs[] =
{
{
.qdisc = "pfifo",
.entry = "pfifo_enqueue"
},
{
.qdisc = "bfifo",
.entry = "bfifo_enqueue"
}
};
/* Find the entry point of a certain Qdisc */
static inline const char* qdisc_entry_point(const char* qdisc)
{
size_t i;
for (i = 0; i < sizeof(known_qdiscs) / sizeof(known_qdiscs[0]); ++i)
{
if (strcmp(known_qdiscs[i].qdisc, qdisc) == 0)
{
return known_qdiscs[i].entry;
}
}
return NULL;
}
/* Initialize the module */
static int __init aqmprobe_entry(void)
{
const char* entry_point;
// Validate arguments
if (qdisc == NULL)
{
printk(KERN_ERR "Qdisc argument is required\n");
return -EINVAL;
}
if ((entry_point = qdisc_entry_point(qdisc)) == NULL)
{
printk(KERN_ERR "Unknown Qdisc: %s\n", qdisc);
return -EINVAL;
}
if (maximum_concurrent_events <= 0)
{
printk(KERN_ERR "Number of concurrent packet events must be 1 or greater\n");
return -EINVAL;
}
if (buffer_size <= 10 || buffer_size > 4096)
{
printk(KERN_ERR "Number of buffered packet event reports must be in range [10-4096]\n");
return -EINVAL;
}
if (flush_frequency < 1 || flush_frequency >= 65536)
{
printk(KERN_ERR "Number of buffered packet event reports before triggering file flush must be in range [1-65536]\n");
return -EINVAL;
}
// Initialize message queue
if (mq_create(buffer_size, flush_frequency))
{
printk(KERN_ERR "Failed to allocate packet event report buffer\n");
return -ENOMEM;
}
// Create report file
if (fo_init())
{
printk(KERN_ERR "Failed to create report file\n");
return -ENOMEM;
}
// Attach probe
qp_attach(entry_point, maximum_concurrent_events);
#ifdef DEBUG
printk(KERN_INFO "Probe registered on Qdisc=%s (flush_freq=%d buf_size=%d)\n", qdisc, flush_frequency, buffer_size);
printk(KERN_DEBUG "sizeof(struct msg)=%lu\n", sizeof(struct msg));
#else
printk(KERN_INFO "Probe registered on Qdisc=%s\n", qdisc);
#endif
return 0;
}
module_init(aqmprobe_entry);
/* Clean up the module */
static void __exit aqmprobe_exit(void)
{
int nmissed;
nmissed = qp_detach();
fo_destroy();
mq_destroy();
printk(KERN_INFO "Unregistering probe, missed %d instances\n", nmissed);
}
module_exit(aqmprobe_exit);