drop_monitor.c 8.5 KB

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