drop_monitor.c 10 KB

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