drop_monitor.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Monitoring code for network dropped packet alerts
  3. *
  4. * Copyright (C) 2009 Neil Horman <nhorman@tuxdriver.com>
  5. */
  6. #include <linux/netdevice.h>
  7. #include <linux/etherdevice.h>
  8. #include <linux/string.h>
  9. #include <linux/if_arp.h>
  10. #include <linux/inetdevice.h>
  11. #include <linux/inet.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/netpoll.h>
  14. #include <linux/sched.h>
  15. #include <linux/delay.h>
  16. #include <linux/types.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/netlink.h>
  19. #include <linux/net_dropmon.h>
  20. #include <linux/percpu.h>
  21. #include <linux/timer.h>
  22. #include <linux/bitops.h>
  23. #include <net/genetlink.h>
  24. #include <net/netevent.h>
  25. #include <trace/events/skb.h>
  26. #include <trace/events/napi.h>
  27. #include <asm/unaligned.h>
  28. #define TRACE_ON 1
  29. #define TRACE_OFF 0
  30. static void send_dm_alert(struct work_struct *unused);
  31. /*
  32. * Globals, our netlink socket pointer
  33. * and the work handle that will send up
  34. * netlink alerts
  35. */
  36. static int trace_state = TRACE_OFF;
  37. static spinlock_t trace_state_lock = SPIN_LOCK_UNLOCKED;
  38. struct per_cpu_dm_data {
  39. struct work_struct dm_alert_work;
  40. struct sk_buff *skb;
  41. atomic_t dm_hit_count;
  42. struct timer_list send_timer;
  43. };
  44. struct dm_hw_stat_delta {
  45. struct net_device *dev;
  46. unsigned long last_rx;
  47. struct list_head list;
  48. struct rcu_head rcu;
  49. unsigned long last_drop_val;
  50. };
  51. static struct genl_family net_drop_monitor_family = {
  52. .id = GENL_ID_GENERATE,
  53. .hdrsize = 0,
  54. .name = "NET_DM",
  55. .version = 2,
  56. .maxattr = NET_DM_CMD_MAX,
  57. };
  58. static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
  59. static int dm_hit_limit = 64;
  60. static int dm_delay = 1;
  61. static unsigned long dm_hw_check_delta = 2*HZ;
  62. static LIST_HEAD(hw_stats_list);
  63. static void reset_per_cpu_data(struct per_cpu_dm_data *data)
  64. {
  65. size_t al;
  66. struct net_dm_alert_msg *msg;
  67. struct nlattr *nla;
  68. al = sizeof(struct net_dm_alert_msg);
  69. al += dm_hit_limit * sizeof(struct net_dm_drop_point);
  70. al += sizeof(struct nlattr);
  71. data->skb = genlmsg_new(al, GFP_KERNEL);
  72. genlmsg_put(data->skb, 0, 0, &net_drop_monitor_family,
  73. 0, NET_DM_CMD_ALERT);
  74. nla = nla_reserve(data->skb, NLA_UNSPEC, sizeof(struct net_dm_alert_msg));
  75. msg = nla_data(nla);
  76. memset(msg, 0, al);
  77. atomic_set(&data->dm_hit_count, dm_hit_limit);
  78. }
  79. static void send_dm_alert(struct work_struct *unused)
  80. {
  81. struct sk_buff *skb;
  82. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  83. /*
  84. * Grab the skb we're about to send
  85. */
  86. skb = data->skb;
  87. /*
  88. * Replace it with a new one
  89. */
  90. reset_per_cpu_data(data);
  91. /*
  92. * Ship it!
  93. */
  94. genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
  95. }
  96. /*
  97. * This is the timer function to delay the sending of an alert
  98. * in the event that more drops will arrive during the
  99. * hysteresis period. Note that it operates under the timer interrupt
  100. * so we don't need to disable preemption here
  101. */
  102. static void sched_send_work(unsigned long unused)
  103. {
  104. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  105. schedule_work(&data->dm_alert_work);
  106. }
  107. static void trace_drop_common(struct sk_buff *skb, void *location)
  108. {
  109. struct net_dm_alert_msg *msg;
  110. struct nlmsghdr *nlh;
  111. struct nlattr *nla;
  112. int i;
  113. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  114. if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
  115. /*
  116. * we're already at zero, discard this hit
  117. */
  118. goto out;
  119. }
  120. nlh = (struct nlmsghdr *)data->skb->data;
  121. nla = genlmsg_data(nlmsg_data(nlh));
  122. msg = nla_data(nla);
  123. for (i = 0; i < msg->entries; i++) {
  124. if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
  125. msg->points[i].count++;
  126. goto out;
  127. }
  128. }
  129. /*
  130. * We need to create a new entry
  131. */
  132. __nla_reserve_nohdr(data->skb, sizeof(struct net_dm_drop_point));
  133. nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
  134. memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
  135. msg->points[msg->entries].count = 1;
  136. msg->entries++;
  137. if (!timer_pending(&data->send_timer)) {
  138. data->send_timer.expires = jiffies + dm_delay * HZ;
  139. add_timer_on(&data->send_timer, smp_processor_id());
  140. }
  141. out:
  142. return;
  143. }
  144. static void trace_kfree_skb_hit(struct sk_buff *skb, void *location)
  145. {
  146. trace_drop_common(skb, location);
  147. }
  148. static void trace_napi_poll_hit(struct napi_struct *napi)
  149. {
  150. struct dm_hw_stat_delta *new_stat;
  151. /*
  152. * Don't check napi structures with no associated device
  153. */
  154. if (!napi->dev)
  155. return;
  156. rcu_read_lock();
  157. list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
  158. /*
  159. * only add a note to our monitor buffer if:
  160. * 1) this is the dev we received on
  161. * 2) its after the last_rx delta
  162. * 3) our rx_dropped count has gone up
  163. */
  164. if ((new_stat->dev == napi->dev) &&
  165. (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
  166. (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
  167. trace_drop_common(NULL, NULL);
  168. new_stat->last_drop_val = napi->dev->stats.rx_dropped;
  169. new_stat->last_rx = jiffies;
  170. break;
  171. }
  172. }
  173. rcu_read_unlock();
  174. }
  175. static void free_dm_hw_stat(struct rcu_head *head)
  176. {
  177. struct dm_hw_stat_delta *n;
  178. n = container_of(head, struct dm_hw_stat_delta, rcu);
  179. kfree(n);
  180. }
  181. static int set_all_monitor_traces(int state)
  182. {
  183. int rc = 0;
  184. struct dm_hw_stat_delta *new_stat = NULL;
  185. struct dm_hw_stat_delta *temp;
  186. spin_lock(&trace_state_lock);
  187. switch (state) {
  188. case TRACE_ON:
  189. rc |= register_trace_kfree_skb(trace_kfree_skb_hit);
  190. rc |= register_trace_napi_poll(trace_napi_poll_hit);
  191. break;
  192. case TRACE_OFF:
  193. rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit);
  194. rc |= unregister_trace_napi_poll(trace_napi_poll_hit);
  195. tracepoint_synchronize_unregister();
  196. /*
  197. * Clean the device list
  198. */
  199. list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
  200. if (new_stat->dev == NULL) {
  201. list_del_rcu(&new_stat->list);
  202. call_rcu(&new_stat->rcu, free_dm_hw_stat);
  203. }
  204. }
  205. break;
  206. default:
  207. rc = 1;
  208. break;
  209. }
  210. if (!rc)
  211. trace_state = state;
  212. spin_unlock(&trace_state_lock);
  213. if (rc)
  214. return -EINPROGRESS;
  215. return rc;
  216. }
  217. static int net_dm_cmd_config(struct sk_buff *skb,
  218. struct genl_info *info)
  219. {
  220. return -ENOTSUPP;
  221. }
  222. static int net_dm_cmd_trace(struct sk_buff *skb,
  223. struct genl_info *info)
  224. {
  225. switch (info->genlhdr->cmd) {
  226. case NET_DM_CMD_START:
  227. return set_all_monitor_traces(TRACE_ON);
  228. break;
  229. case NET_DM_CMD_STOP:
  230. return set_all_monitor_traces(TRACE_OFF);
  231. break;
  232. }
  233. return -ENOTSUPP;
  234. }
  235. static int dropmon_net_event(struct notifier_block *ev_block,
  236. unsigned long event, void *ptr)
  237. {
  238. struct net_device *dev = ptr;
  239. struct dm_hw_stat_delta *new_stat = NULL;
  240. struct dm_hw_stat_delta *tmp;
  241. switch (event) {
  242. case NETDEV_REGISTER:
  243. new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
  244. if (!new_stat)
  245. goto out;
  246. new_stat->dev = dev;
  247. new_stat->last_rx = jiffies;
  248. INIT_RCU_HEAD(&new_stat->rcu);
  249. spin_lock(&trace_state_lock);
  250. list_add_rcu(&new_stat->list, &hw_stats_list);
  251. spin_unlock(&trace_state_lock);
  252. break;
  253. case NETDEV_UNREGISTER:
  254. spin_lock(&trace_state_lock);
  255. list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
  256. if (new_stat->dev == dev) {
  257. new_stat->dev = NULL;
  258. if (trace_state == TRACE_OFF) {
  259. list_del_rcu(&new_stat->list);
  260. call_rcu(&new_stat->rcu, free_dm_hw_stat);
  261. break;
  262. }
  263. }
  264. }
  265. spin_unlock(&trace_state_lock);
  266. break;
  267. }
  268. out:
  269. return NOTIFY_DONE;
  270. }
  271. static struct genl_ops dropmon_ops[] = {
  272. {
  273. .cmd = NET_DM_CMD_CONFIG,
  274. .doit = net_dm_cmd_config,
  275. },
  276. {
  277. .cmd = NET_DM_CMD_START,
  278. .doit = net_dm_cmd_trace,
  279. },
  280. {
  281. .cmd = NET_DM_CMD_STOP,
  282. .doit = net_dm_cmd_trace,
  283. },
  284. };
  285. static struct notifier_block dropmon_net_notifier = {
  286. .notifier_call = dropmon_net_event
  287. };
  288. static int __init init_net_drop_monitor(void)
  289. {
  290. int cpu;
  291. int rc, i, ret;
  292. struct per_cpu_dm_data *data;
  293. printk(KERN_INFO "Initalizing network drop monitor service\n");
  294. if (sizeof(void *) > 8) {
  295. printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
  296. return -ENOSPC;
  297. }
  298. if (genl_register_family(&net_drop_monitor_family) < 0) {
  299. printk(KERN_ERR "Could not create drop monitor netlink family\n");
  300. return -EFAULT;
  301. }
  302. rc = -EFAULT;
  303. for (i = 0; i < ARRAY_SIZE(dropmon_ops); i++) {
  304. ret = genl_register_ops(&net_drop_monitor_family,
  305. &dropmon_ops[i]);
  306. if (ret) {
  307. printk(KERN_CRIT "Failed to register operation %d\n",
  308. dropmon_ops[i].cmd);
  309. goto out_unreg;
  310. }
  311. }
  312. rc = register_netdevice_notifier(&dropmon_net_notifier);
  313. if (rc < 0) {
  314. printk(KERN_CRIT "Failed to register netdevice notifier\n");
  315. goto out_unreg;
  316. }
  317. rc = 0;
  318. for_each_present_cpu(cpu) {
  319. data = &per_cpu(dm_cpu_data, cpu);
  320. reset_per_cpu_data(data);
  321. INIT_WORK(&data->dm_alert_work, send_dm_alert);
  322. init_timer(&data->send_timer);
  323. data->send_timer.data = cpu;
  324. data->send_timer.function = sched_send_work;
  325. }
  326. goto out;
  327. out_unreg:
  328. genl_unregister_family(&net_drop_monitor_family);
  329. out:
  330. return rc;
  331. }
  332. late_initcall(init_net_drop_monitor);