drop_monitor.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Monitoring code for network dropped packet alerts
  3. *
  4. * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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 <linux/slab.h>
  25. #include <linux/module.h>
  26. #include <net/genetlink.h>
  27. #include <net/netevent.h>
  28. #include <trace/events/skb.h>
  29. #include <trace/events/napi.h>
  30. #include <asm/unaligned.h>
  31. #define TRACE_ON 1
  32. #define TRACE_OFF 0
  33. /*
  34. * Globals, our netlink socket pointer
  35. * and the work handle that will send up
  36. * netlink alerts
  37. */
  38. static int trace_state = TRACE_OFF;
  39. static DEFINE_MUTEX(trace_state_mutex);
  40. struct per_cpu_dm_data {
  41. spinlock_t lock;
  42. struct sk_buff *skb;
  43. struct work_struct dm_alert_work;
  44. struct timer_list send_timer;
  45. };
  46. struct dm_hw_stat_delta {
  47. struct net_device *dev;
  48. unsigned long last_rx;
  49. struct list_head list;
  50. struct rcu_head rcu;
  51. unsigned long last_drop_val;
  52. };
  53. static struct genl_family net_drop_monitor_family = {
  54. .id = GENL_ID_GENERATE,
  55. .hdrsize = 0,
  56. .name = "NET_DM",
  57. .version = 2,
  58. };
  59. 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;
  62. static unsigned long dm_hw_check_delta = 2*HZ;
  63. static LIST_HEAD(hw_stats_list);
  64. static struct sk_buff *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. struct sk_buff *skb;
  70. unsigned long flags;
  71. al = sizeof(struct net_dm_alert_msg);
  72. al += dm_hit_limit * sizeof(struct net_dm_drop_point);
  73. al += sizeof(struct nlattr);
  74. skb = genlmsg_new(al, GFP_KERNEL);
  75. if (skb) {
  76. genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
  77. 0, NET_DM_CMD_ALERT);
  78. nla = nla_reserve(skb, NLA_UNSPEC,
  79. sizeof(struct net_dm_alert_msg));
  80. msg = nla_data(nla);
  81. memset(msg, 0, al);
  82. } else {
  83. mod_timer(&data->send_timer, jiffies + HZ / 10);
  84. }
  85. spin_lock_irqsave(&data->lock, flags);
  86. swap(data->skb, skb);
  87. spin_unlock_irqrestore(&data->lock, flags);
  88. return skb;
  89. }
  90. static struct genl_multicast_group dropmon_mcgrps[] = {
  91. { .name = "events", },
  92. };
  93. static void send_dm_alert(struct work_struct *work)
  94. {
  95. struct sk_buff *skb;
  96. struct per_cpu_dm_data *data;
  97. data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
  98. skb = reset_per_cpu_data(data);
  99. if (skb)
  100. genlmsg_multicast(&net_drop_monitor_family, skb, 0,
  101. 0, GFP_KERNEL);
  102. }
  103. /*
  104. * This is the timer function to delay the sending of an alert
  105. * in the event that more drops will arrive during the
  106. * hysteresis period.
  107. */
  108. static void sched_send_work(unsigned long _data)
  109. {
  110. struct per_cpu_dm_data *data = (struct per_cpu_dm_data *)_data;
  111. schedule_work(&data->dm_alert_work);
  112. }
  113. static void trace_drop_common(struct sk_buff *skb, void *location)
  114. {
  115. struct net_dm_alert_msg *msg;
  116. struct nlmsghdr *nlh;
  117. struct nlattr *nla;
  118. int i;
  119. struct sk_buff *dskb;
  120. struct per_cpu_dm_data *data;
  121. unsigned long flags;
  122. local_irq_save(flags);
  123. data = this_cpu_ptr(&dm_cpu_data);
  124. spin_lock(&data->lock);
  125. dskb = data->skb;
  126. if (!dskb)
  127. goto out;
  128. nlh = (struct nlmsghdr *)dskb->data;
  129. nla = genlmsg_data(nlmsg_data(nlh));
  130. msg = nla_data(nla);
  131. for (i = 0; i < msg->entries; i++) {
  132. if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
  133. msg->points[i].count++;
  134. goto out;
  135. }
  136. }
  137. if (msg->entries == dm_hit_limit)
  138. goto out;
  139. /*
  140. * We need to create a new entry
  141. */
  142. __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
  143. nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
  144. memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
  145. msg->points[msg->entries].count = 1;
  146. msg->entries++;
  147. if (!timer_pending(&data->send_timer)) {
  148. data->send_timer.expires = jiffies + dm_delay * HZ;
  149. add_timer(&data->send_timer);
  150. }
  151. out:
  152. spin_unlock_irqrestore(&data->lock, flags);
  153. }
  154. static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
  155. {
  156. trace_drop_common(skb, location);
  157. }
  158. static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi)
  159. {
  160. struct dm_hw_stat_delta *new_stat;
  161. /*
  162. * Don't check napi structures with no associated device
  163. */
  164. if (!napi->dev)
  165. return;
  166. rcu_read_lock();
  167. list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
  168. /*
  169. * only add a note to our monitor buffer if:
  170. * 1) this is the dev we received on
  171. * 2) its after the last_rx delta
  172. * 3) our rx_dropped count has gone up
  173. */
  174. if ((new_stat->dev == napi->dev) &&
  175. (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
  176. (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
  177. trace_drop_common(NULL, NULL);
  178. new_stat->last_drop_val = napi->dev->stats.rx_dropped;
  179. new_stat->last_rx = jiffies;
  180. break;
  181. }
  182. }
  183. rcu_read_unlock();
  184. }
  185. static int set_all_monitor_traces(int state)
  186. {
  187. int rc = 0;
  188. struct dm_hw_stat_delta *new_stat = NULL;
  189. struct dm_hw_stat_delta *temp;
  190. mutex_lock(&trace_state_mutex);
  191. if (state == trace_state) {
  192. rc = -EAGAIN;
  193. goto out_unlock;
  194. }
  195. switch (state) {
  196. case TRACE_ON:
  197. if (!try_module_get(THIS_MODULE)) {
  198. rc = -ENODEV;
  199. break;
  200. }
  201. rc |= register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
  202. rc |= register_trace_napi_poll(trace_napi_poll_hit, NULL);
  203. break;
  204. case TRACE_OFF:
  205. rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
  206. rc |= unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
  207. tracepoint_synchronize_unregister();
  208. /*
  209. * Clean the device list
  210. */
  211. list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
  212. if (new_stat->dev == NULL) {
  213. list_del_rcu(&new_stat->list);
  214. kfree_rcu(new_stat, rcu);
  215. }
  216. }
  217. module_put(THIS_MODULE);
  218. break;
  219. default:
  220. rc = 1;
  221. break;
  222. }
  223. if (!rc)
  224. trace_state = state;
  225. else
  226. rc = -EINPROGRESS;
  227. out_unlock:
  228. mutex_unlock(&trace_state_mutex);
  229. return rc;
  230. }
  231. static int net_dm_cmd_config(struct sk_buff *skb,
  232. struct genl_info *info)
  233. {
  234. return -ENOTSUPP;
  235. }
  236. static int net_dm_cmd_trace(struct sk_buff *skb,
  237. struct genl_info *info)
  238. {
  239. switch (info->genlhdr->cmd) {
  240. case NET_DM_CMD_START:
  241. return set_all_monitor_traces(TRACE_ON);
  242. case NET_DM_CMD_STOP:
  243. return set_all_monitor_traces(TRACE_OFF);
  244. }
  245. return -ENOTSUPP;
  246. }
  247. static int dropmon_net_event(struct notifier_block *ev_block,
  248. unsigned long event, void *ptr)
  249. {
  250. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  251. struct dm_hw_stat_delta *new_stat = NULL;
  252. struct dm_hw_stat_delta *tmp;
  253. switch (event) {
  254. case NETDEV_REGISTER:
  255. new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
  256. if (!new_stat)
  257. goto out;
  258. new_stat->dev = dev;
  259. new_stat->last_rx = jiffies;
  260. mutex_lock(&trace_state_mutex);
  261. list_add_rcu(&new_stat->list, &hw_stats_list);
  262. mutex_unlock(&trace_state_mutex);
  263. break;
  264. case NETDEV_UNREGISTER:
  265. mutex_lock(&trace_state_mutex);
  266. list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
  267. if (new_stat->dev == dev) {
  268. new_stat->dev = NULL;
  269. if (trace_state == TRACE_OFF) {
  270. list_del_rcu(&new_stat->list);
  271. kfree_rcu(new_stat, rcu);
  272. break;
  273. }
  274. }
  275. }
  276. mutex_unlock(&trace_state_mutex);
  277. break;
  278. }
  279. out:
  280. return NOTIFY_DONE;
  281. }
  282. static const struct genl_ops dropmon_ops[] = {
  283. {
  284. .cmd = NET_DM_CMD_CONFIG,
  285. .doit = net_dm_cmd_config,
  286. },
  287. {
  288. .cmd = NET_DM_CMD_START,
  289. .doit = net_dm_cmd_trace,
  290. },
  291. {
  292. .cmd = NET_DM_CMD_STOP,
  293. .doit = net_dm_cmd_trace,
  294. },
  295. };
  296. static struct notifier_block dropmon_net_notifier = {
  297. .notifier_call = dropmon_net_event
  298. };
  299. static int __init init_net_drop_monitor(void)
  300. {
  301. struct per_cpu_dm_data *data;
  302. int cpu, rc;
  303. pr_info("Initializing network drop monitor service\n");
  304. if (sizeof(void *) > 8) {
  305. pr_err("Unable to store program counters on this arch, Drop monitor failed\n");
  306. return -ENOSPC;
  307. }
  308. rc = genl_register_family_with_ops_groups(&net_drop_monitor_family,
  309. dropmon_ops, dropmon_mcgrps);
  310. if (rc) {
  311. pr_err("Could not create drop monitor netlink family\n");
  312. return rc;
  313. }
  314. WARN_ON(net_drop_monitor_family.mcgrp_offset != NET_DM_GRP_ALERT);
  315. rc = register_netdevice_notifier(&dropmon_net_notifier);
  316. if (rc < 0) {
  317. pr_crit("Failed to register netdevice notifier\n");
  318. goto out_unreg;
  319. }
  320. rc = 0;
  321. for_each_possible_cpu(cpu) {
  322. data = &per_cpu(dm_cpu_data, cpu);
  323. INIT_WORK(&data->dm_alert_work, send_dm_alert);
  324. init_timer(&data->send_timer);
  325. data->send_timer.data = (unsigned long)data;
  326. data->send_timer.function = sched_send_work;
  327. spin_lock_init(&data->lock);
  328. reset_per_cpu_data(data);
  329. }
  330. goto out;
  331. out_unreg:
  332. genl_unregister_family(&net_drop_monitor_family);
  333. out:
  334. return rc;
  335. }
  336. static void exit_net_drop_monitor(void)
  337. {
  338. struct per_cpu_dm_data *data;
  339. int cpu;
  340. BUG_ON(unregister_netdevice_notifier(&dropmon_net_notifier));
  341. /*
  342. * Because of the module_get/put we do in the trace state change path
  343. * we are guarnateed not to have any current users when we get here
  344. * all we need to do is make sure that we don't have any running timers
  345. * or pending schedule calls
  346. */
  347. for_each_possible_cpu(cpu) {
  348. data = &per_cpu(dm_cpu_data, cpu);
  349. del_timer_sync(&data->send_timer);
  350. cancel_work_sync(&data->dm_alert_work);
  351. /*
  352. * At this point, we should have exclusive access
  353. * to this struct and can free the skb inside it
  354. */
  355. kfree_skb(data->skb);
  356. }
  357. BUG_ON(genl_unregister_family(&net_drop_monitor_family));
  358. }
  359. module_init(init_net_drop_monitor);
  360. module_exit(exit_net_drop_monitor);
  361. MODULE_LICENSE("GPL v2");
  362. MODULE_AUTHOR("Neil Horman <nhorman@tuxdriver.com>");
  363. MODULE_ALIAS_GENL_FAMILY("NET_DM");