drop_monitor: Update netlink protocol to include netlink attribute header in alert...
[linux-2.6.git] / net / core / drop_monitor.c
1 /*
2  * Monitoring code for network dropped packet alerts
3  *
4  * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
5  */
6
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/string.h>
10 #include <linux/if_arp.h>
11 #include <linux/inetdevice.h>
12 #include <linux/inet.h>
13 #include <linux/interrupt.h>
14 #include <linux/netpoll.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <linux/types.h>
18 #include <linux/workqueue.h>
19 #include <linux/netlink.h>
20 #include <linux/net_dropmon.h>
21 #include <linux/percpu.h>
22 #include <linux/timer.h>
23 #include <linux/bitops.h>
24 #include <net/genetlink.h>
25
26 #include <trace/skb.h>
27
28 #include <asm/unaligned.h>
29
30 #define TRACE_ON 1
31 #define TRACE_OFF 0
32
33 static void send_dm_alert(struct work_struct *unused);
34
35
36 /*
37  * Globals, our netlink socket pointer
38  * and the work handle that will send up
39  * netlink alerts
40  */
41 struct sock *dm_sock;
42
43 struct per_cpu_dm_data {
44         struct work_struct dm_alert_work;
45         struct sk_buff *skb;
46         atomic_t dm_hit_count;
47         struct timer_list send_timer;
48 };
49
50 static struct genl_family net_drop_monitor_family = {
51         .id             = GENL_ID_GENERATE,
52         .hdrsize        = 0,
53         .name           = "NET_DM",
54         .version        = 2,
55         .maxattr        = NET_DM_CMD_MAX,
56 };
57
58 static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
59
60 static int dm_hit_limit = 64;
61 static int dm_delay = 1;
62
63
64 static void reset_per_cpu_data(struct per_cpu_dm_data *data)
65 {
66         size_t al;
67         struct net_dm_alert_msg *msg;
68         struct nlattr *nla;
69
70         al = sizeof(struct net_dm_alert_msg);
71         al += dm_hit_limit * sizeof(struct net_dm_drop_point);
72         al += sizeof(struct nlattr);
73
74         data->skb = genlmsg_new(al, GFP_KERNEL);
75         genlmsg_put(data->skb, 0, 0, &net_drop_monitor_family,
76                         0, NET_DM_CMD_ALERT);
77         nla = nla_reserve(data->skb, NLA_UNSPEC, sizeof(struct net_dm_alert_msg));
78         msg = nla_data(nla);
79         memset(msg, 0, al);
80         atomic_set(&data->dm_hit_count, dm_hit_limit);
81 }
82
83 static void send_dm_alert(struct work_struct *unused)
84 {
85         struct sk_buff *skb;
86         struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
87
88         /*
89          * Grab the skb we're about to send
90          */
91         skb = data->skb;
92
93         /*
94          * Replace it with a new one
95          */
96         reset_per_cpu_data(data);
97
98         /*
99          * Ship it!
100          */
101         genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
102
103 }
104
105 /*
106  * This is the timer function to delay the sending of an alert
107  * in the event that more drops will arrive during the
108  * hysteresis period.  Note that it operates under the timer interrupt
109  * so we don't need to disable preemption here
110  */
111 static void sched_send_work(unsigned long unused)
112 {
113         struct per_cpu_dm_data *data =  &__get_cpu_var(dm_cpu_data);
114
115         schedule_work(&data->dm_alert_work);
116 }
117
118 static void trace_kfree_skb_hit(struct sk_buff *skb, void *location)
119 {
120         struct net_dm_alert_msg *msg;
121         struct nlmsghdr *nlh;
122         struct nlattr *nla;
123         int i;
124         struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
125
126
127         if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
128                 /*
129                  * we're already at zero, discard this hit
130                  */
131                 goto out;
132         }
133
134         nlh = (struct nlmsghdr *)data->skb->data;
135         nla = genlmsg_data(nlmsg_data(nlh));
136         msg = nla_data(nla);
137         for (i = 0; i < msg->entries; i++) {
138                 if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
139                         msg->points[i].count++;
140                         goto out;
141                 }
142         }
143
144         /*
145          * We need to create a new entry
146          */
147         __nla_reserve_nohdr(data->skb, sizeof(struct net_dm_drop_point));
148         nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
149         memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
150         msg->points[msg->entries].count = 1;
151         msg->entries++;
152
153         if (!timer_pending(&data->send_timer)) {
154                 data->send_timer.expires = jiffies + dm_delay * HZ;
155                 add_timer_on(&data->send_timer, smp_processor_id());
156         }
157
158 out:
159         return;
160 }
161
162 static int set_all_monitor_traces(int state)
163 {
164         int rc = 0;
165
166         switch (state) {
167         case TRACE_ON:
168                 rc |= register_trace_kfree_skb(trace_kfree_skb_hit);
169                 break;
170         case TRACE_OFF:
171                 rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit);
172
173                 tracepoint_synchronize_unregister();
174                 break;
175         default:
176                 rc = 1;
177                 break;
178         }
179
180         if (rc)
181                 return -EINPROGRESS;
182         return rc;
183 }
184
185
186 static int net_dm_cmd_config(struct sk_buff *skb,
187                         struct genl_info *info)
188 {
189         return -ENOTSUPP;
190 }
191
192 static int net_dm_cmd_trace(struct sk_buff *skb,
193                         struct genl_info *info)
194 {
195         switch (info->genlhdr->cmd) {
196         case NET_DM_CMD_START:
197                 return set_all_monitor_traces(TRACE_ON);
198                 break;
199         case NET_DM_CMD_STOP:
200                 return set_all_monitor_traces(TRACE_OFF);
201                 break;
202         }
203
204         return -ENOTSUPP;
205 }
206
207
208 static struct genl_ops dropmon_ops[] = {
209         {
210                 .cmd = NET_DM_CMD_CONFIG,
211                 .doit = net_dm_cmd_config,
212         },
213         {
214                 .cmd = NET_DM_CMD_START,
215                 .doit = net_dm_cmd_trace,
216         },
217         {
218                 .cmd = NET_DM_CMD_STOP,
219                 .doit = net_dm_cmd_trace,
220         },
221 };
222
223 static int __init init_net_drop_monitor(void)
224 {
225         int cpu;
226         int rc, i, ret;
227         struct per_cpu_dm_data *data;
228         printk(KERN_INFO "Initalizing network drop monitor service\n");
229
230         if (sizeof(void *) > 8) {
231                 printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
232                 return -ENOSPC;
233         }
234
235         if (genl_register_family(&net_drop_monitor_family) < 0) {
236                 printk(KERN_ERR "Could not create drop monitor netlink family\n");
237                 return -EFAULT;
238         }
239
240         rc = -EFAULT;
241
242         for (i = 0; i < ARRAY_SIZE(dropmon_ops); i++) {
243                 ret = genl_register_ops(&net_drop_monitor_family,
244                                         &dropmon_ops[i]);
245                 if (ret) {
246                         printk(KERN_CRIT "failed to register operation %d\n",
247                                 dropmon_ops[i].cmd);
248                         goto out_unreg;
249                 }
250         }
251
252         rc = 0;
253
254         for_each_present_cpu(cpu) {
255                 data = &per_cpu(dm_cpu_data, cpu);
256                 reset_per_cpu_data(data);
257                 INIT_WORK(&data->dm_alert_work, send_dm_alert);
258                 init_timer(&data->send_timer);
259                 data->send_timer.data = cpu;
260                 data->send_timer.function = sched_send_work;
261         }
262         goto out;
263
264 out_unreg:
265         genl_unregister_family(&net_drop_monitor_family);
266 out:
267         return rc;
268 }
269
270 late_initcall(init_net_drop_monitor);