2 * Monitoring code for network dropped packet alerts
4 * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
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>
26 #include <trace/skb.h>
28 #include <asm/unaligned.h>
33 static void send_dm_alert(struct work_struct
*unused
);
37 * Globals, our netlink socket pointer
38 * and the work handle that will send up
43 struct per_cpu_dm_data
{
44 struct work_struct dm_alert_work
;
46 atomic_t dm_hit_count
;
47 struct timer_list send_timer
;
50 static struct genl_family net_drop_monitor_family
= {
51 .id
= GENL_ID_GENERATE
,
55 .maxattr
= NET_DM_CMD_MAX
,
58 static DEFINE_PER_CPU(struct per_cpu_dm_data
, dm_cpu_data
);
60 static int dm_hit_limit
= 64;
61 static int dm_delay
= 1;
64 static void reset_per_cpu_data(struct per_cpu_dm_data
*data
)
67 struct net_dm_alert_msg
*msg
;
69 al
= sizeof(struct net_dm_alert_msg
);
70 al
+= dm_hit_limit
* sizeof(struct net_dm_drop_point
);
71 data
->skb
= genlmsg_new(al
, GFP_KERNEL
);
72 genlmsg_put(data
->skb
, 0, 0, &net_drop_monitor_family
,
74 msg
= __nla_reserve_nohdr(data
->skb
, sizeof(struct net_dm_alert_msg
));
76 atomic_set(&data
->dm_hit_count
, dm_hit_limit
);
79 static void send_dm_alert(struct work_struct
*unused
)
82 struct per_cpu_dm_data
*data
= &__get_cpu_var(dm_cpu_data
);
85 * Grab the skb we're about to send
90 * Replace it with a new one
92 reset_per_cpu_data(data
);
97 genlmsg_multicast(skb
, 0, NET_DM_GRP_ALERT
, GFP_KERNEL
);
102 * This is the timer function to delay the sending of an alert
103 * in the event that more drops will arrive during the
104 * hysteresis period. Note that it operates under the timer interrupt
105 * so we don't need to disable preemption here
107 static void sched_send_work(unsigned long unused
)
109 struct per_cpu_dm_data
*data
= &__get_cpu_var(dm_cpu_data
);
111 schedule_work(&data
->dm_alert_work
);
114 static void trace_kfree_skb_hit(struct sk_buff
*skb
, void *location
)
116 struct net_dm_alert_msg
*msg
;
117 struct nlmsghdr
*nlh
;
119 struct per_cpu_dm_data
*data
= &__get_cpu_var(dm_cpu_data
);
122 if (!atomic_add_unless(&data
->dm_hit_count
, -1, 0)) {
124 * we're already at zero, discard this hit
129 nlh
= (struct nlmsghdr
*)data
->skb
->data
;
130 msg
= genlmsg_data(nlmsg_data(nlh
));
131 for (i
= 0; i
< msg
->entries
; i
++) {
132 if (!memcmp(&location
, msg
->points
[i
].pc
, sizeof(void *))) {
133 msg
->points
[i
].count
++;
139 * We need to create a new entry
141 __nla_reserve_nohdr(data
->skb
, sizeof(struct net_dm_drop_point
));
142 memcpy(msg
->points
[msg
->entries
].pc
, &location
, sizeof(void *));
143 msg
->points
[msg
->entries
].count
= 1;
146 if (!timer_pending(&data
->send_timer
)) {
147 data
->send_timer
.expires
= jiffies
+ dm_delay
* HZ
;
148 add_timer_on(&data
->send_timer
, smp_processor_id());
155 static int set_all_monitor_traces(int state
)
161 rc
|= register_trace_kfree_skb(trace_kfree_skb_hit
);
164 rc
|= unregister_trace_kfree_skb(trace_kfree_skb_hit
);
166 tracepoint_synchronize_unregister();
179 static int net_dm_cmd_config(struct sk_buff
*skb
,
180 struct genl_info
*info
)
185 static int net_dm_cmd_trace(struct sk_buff
*skb
,
186 struct genl_info
*info
)
188 switch (info
->genlhdr
->cmd
) {
189 case NET_DM_CMD_START
:
190 return set_all_monitor_traces(TRACE_ON
);
192 case NET_DM_CMD_STOP
:
193 return set_all_monitor_traces(TRACE_OFF
);
201 static struct genl_ops dropmon_ops
[] = {
203 .cmd
= NET_DM_CMD_CONFIG
,
204 .doit
= net_dm_cmd_config
,
207 .cmd
= NET_DM_CMD_START
,
208 .doit
= net_dm_cmd_trace
,
211 .cmd
= NET_DM_CMD_STOP
,
212 .doit
= net_dm_cmd_trace
,
216 static int __init
init_net_drop_monitor(void)
220 struct per_cpu_dm_data
*data
;
221 printk(KERN_INFO
"Initalizing network drop monitor service\n");
223 if (sizeof(void *) > 8) {
224 printk(KERN_ERR
"Unable to store program counters on this arch, Drop monitor failed\n");
228 if (genl_register_family(&net_drop_monitor_family
) < 0) {
229 printk(KERN_ERR
"Could not create drop monitor netlink family\n");
235 for (i
= 0; i
< ARRAY_SIZE(dropmon_ops
); i
++) {
236 ret
= genl_register_ops(&net_drop_monitor_family
,
239 printk(KERN_CRIT
"failed to register operation %d\n",
247 for_each_present_cpu(cpu
) {
248 data
= &per_cpu(dm_cpu_data
, cpu
);
249 reset_per_cpu_data(data
);
250 INIT_WORK(&data
->dm_alert_work
, send_dm_alert
);
251 init_timer(&data
->send_timer
);
252 data
->send_timer
.data
= cpu
;
253 data
->send_timer
.function
= sched_send_work
;
258 genl_unregister_family(&net_drop_monitor_family
);
263 late_initcall(init_net_drop_monitor
);