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
;
70 al
= sizeof(struct net_dm_alert_msg
);
71 al
+= dm_hit_limit
* sizeof(struct net_dm_drop_point
);
72 al
+= sizeof(struct nlattr
);
74 data
->skb
= genlmsg_new(al
, GFP_KERNEL
);
75 genlmsg_put(data
->skb
, 0, 0, &net_drop_monitor_family
,
77 nla
= nla_reserve(data
->skb
, NLA_UNSPEC
, sizeof(struct net_dm_alert_msg
));
80 atomic_set(&data
->dm_hit_count
, dm_hit_limit
);
83 static void send_dm_alert(struct work_struct
*unused
)
86 struct per_cpu_dm_data
*data
= &__get_cpu_var(dm_cpu_data
);
89 * Grab the skb we're about to send
94 * Replace it with a new one
96 reset_per_cpu_data(data
);
101 genlmsg_multicast(skb
, 0, NET_DM_GRP_ALERT
, GFP_KERNEL
);
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
111 static void sched_send_work(unsigned long unused
)
113 struct per_cpu_dm_data
*data
= &__get_cpu_var(dm_cpu_data
);
115 schedule_work(&data
->dm_alert_work
);
118 static void trace_kfree_skb_hit(struct sk_buff
*skb
, void *location
)
120 struct net_dm_alert_msg
*msg
;
121 struct nlmsghdr
*nlh
;
124 struct per_cpu_dm_data
*data
= &__get_cpu_var(dm_cpu_data
);
127 if (!atomic_add_unless(&data
->dm_hit_count
, -1, 0)) {
129 * we're already at zero, discard this hit
134 nlh
= (struct nlmsghdr
*)data
->skb
->data
;
135 nla
= genlmsg_data(nlmsg_data(nlh
));
137 for (i
= 0; i
< msg
->entries
; i
++) {
138 if (!memcmp(&location
, msg
->points
[i
].pc
, sizeof(void *))) {
139 msg
->points
[i
].count
++;
145 * We need to create a new entry
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;
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());
162 static int set_all_monitor_traces(int state
)
168 rc
|= register_trace_kfree_skb(trace_kfree_skb_hit
);
171 rc
|= unregister_trace_kfree_skb(trace_kfree_skb_hit
);
173 tracepoint_synchronize_unregister();
186 static int net_dm_cmd_config(struct sk_buff
*skb
,
187 struct genl_info
*info
)
192 static int net_dm_cmd_trace(struct sk_buff
*skb
,
193 struct genl_info
*info
)
195 switch (info
->genlhdr
->cmd
) {
196 case NET_DM_CMD_START
:
197 return set_all_monitor_traces(TRACE_ON
);
199 case NET_DM_CMD_STOP
:
200 return set_all_monitor_traces(TRACE_OFF
);
208 static struct genl_ops dropmon_ops
[] = {
210 .cmd
= NET_DM_CMD_CONFIG
,
211 .doit
= net_dm_cmd_config
,
214 .cmd
= NET_DM_CMD_START
,
215 .doit
= net_dm_cmd_trace
,
218 .cmd
= NET_DM_CMD_STOP
,
219 .doit
= net_dm_cmd_trace
,
223 static int __init
init_net_drop_monitor(void)
227 struct per_cpu_dm_data
*data
;
228 printk(KERN_INFO
"Initalizing network drop monitor service\n");
230 if (sizeof(void *) > 8) {
231 printk(KERN_ERR
"Unable to store program counters on this arch, Drop monitor failed\n");
235 if (genl_register_family(&net_drop_monitor_family
) < 0) {
236 printk(KERN_ERR
"Could not create drop monitor netlink family\n");
242 for (i
= 0; i
< ARRAY_SIZE(dropmon_ops
); i
++) {
243 ret
= genl_register_ops(&net_drop_monitor_family
,
246 printk(KERN_CRIT
"failed to register operation %d\n",
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
;
265 genl_unregister_family(&net_drop_monitor_family
);
270 late_initcall(init_net_drop_monitor
);