Skip to content

Commit

Permalink
drop_monitor: convert to modular building
Browse files Browse the repository at this point in the history
When I first wrote drop monitor I wrote it to just build monolithically.  There
is no reason it can't be built modularly as well, so lets give it that
flexibiity.

I've tested this by building it as both a module and monolithically, and it
seems to work quite well

Change notes:

v2)
* fixed for_each_present_cpu loops to be more correct as per Eric D.
* Converted exit path failures to BUG_ON as per Ben H.

v3)
* Converted del_timer to del_timer_sync to close race noted by Ben H.

Signed-off-by: Neil Horman <[email protected]>
CC: "David S. Miller" <[email protected]>
CC: Eric Dumazet <[email protected]>
CC: Ben Hutchings <[email protected]>
Reviewed-by: Ben Hutchings <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
nhorman authored and davem330 committed May 17, 2012
1 parent a1c7fff commit cad456d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion net/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ config NET_TCPPROBE
module will be called tcp_probe.

config NET_DROP_MONITOR
boolean "Network packet drop alerting service"
tristate "Network packet drop alerting service"
depends on INET && EXPERIMENTAL && TRACEPOINTS
---help---
This feature provides an alerting service to userspace in the
Expand Down
46 changes: 44 additions & 2 deletions net/core/drop_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <linux/timer.h>
#include <linux/bitops.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <net/genetlink.h>
#include <net/netevent.h>

Expand Down Expand Up @@ -263,9 +264,15 @@ static int set_all_monitor_traces(int state)

switch (state) {
case TRACE_ON:
if (!try_module_get(THIS_MODULE)) {
rc = -ENODEV;
break;
}

rc |= register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
rc |= register_trace_napi_poll(trace_napi_poll_hit, NULL);
break;

case TRACE_OFF:
rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
rc |= unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
Expand All @@ -281,6 +288,9 @@ static int set_all_monitor_traces(int state)
kfree_rcu(new_stat, rcu);
}
}

module_put(THIS_MODULE);

break;
default:
rc = 1;
Expand Down Expand Up @@ -406,7 +416,7 @@ static int __init init_net_drop_monitor(void)

rc = 0;

for_each_present_cpu(cpu) {
for_each_possible_cpu(cpu) {
data = &per_cpu(dm_cpu_data, cpu);
data->cpu = cpu;
INIT_WORK(&data->dm_alert_work, send_dm_alert);
Expand All @@ -425,4 +435,36 @@ static int __init init_net_drop_monitor(void)
return rc;
}

late_initcall(init_net_drop_monitor);
static void exit_net_drop_monitor(void)
{
struct per_cpu_dm_data *data;
int cpu;

BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier));

/*
* Because of the module_get/put we do in the trace state change path
* we are guarnateed not to have any current users when we get here
* all we need to do is make sure that we don't have any running timers
* or pending schedule calls
*/

for_each_possible_cpu(cpu) {
data = &per_cpu(dm_cpu_data, cpu);
del_timer_sync(&data->send_timer);
cancel_work_sync(&data->dm_alert_work);
/*
* At this point, we should have exclusive access
* to this struct and can free the skb inside it
*/
kfree_skb(data->skb);
}

BUG_ON(genl_unregister_family(&net_drop_monitor_family));
}

module_init(init_net_drop_monitor);
module_exit(exit_net_drop_monitor);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Neil Horman <[email protected]>");

0 comments on commit cad456d

Please sign in to comment.