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 <linux/slab.h>
  24. #include <net/genetlink.h>
  25. #include <net/netevent.h>
  26. #include <trace/events/skb.h>
  27. #include <trace/events/napi.h>
  28. #include <asm/unaligned.h>
  29. #define TRACE_ON 1
  30. #define TRACE_OFF 0
  31. static void send_dm_alert(struct work_struct *unused);
  32. /*
  33. * Globals, our netlink socket pointer
  34. * and the work handle that will send up
  35. * netlink alerts
  36. */
  37. static int trace_state = TRACE_OFF;
  38. static DEFINE_SPINLOCK(trace_state_lock);
  39. struct per_cpu_dm_data {
  40. struct work_struct dm_alert_work;
  41. struct sk_buff *skb;
  42. atomic_t dm_hit_count;
  43. struct timer_list send_timer;
  44. };
  45. struct dm_hw_stat_delta {
  46. struct net_device *dev;
  47. unsigned long last_rx;
  48. struct list_head list;
  49. struct rcu_head rcu;
  50. unsigned long last_drop_val;
  51. };
  52. static struct genl_family net_drop_monitor_family = {
  53. .id = GENL_ID_GENERATE,
  54. .hdrsize = 0,
  55. .name = "NET_DM",
  56. .version = 2,
  57. .maxattr = NET_DM_CMD_MAX,
  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 void 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. al = sizeof(struct net_dm_alert_msg);
  70. al += dm_hit_limit * sizeof(struct net_dm_drop_point);
  71. al += sizeof(struct nlattr);
  72. data->skb = genlmsg_new(al, GFP_KERNEL);
  73. genlmsg_put(data->skb, 0, 0, &net_drop_monitor_family,
  74. 0, NET_DM_CMD_ALERT);
  75. nla = nla_reserve(data->skb, NLA_UNSPEC, sizeof(struct net_dm_alert_msg));
  76. msg = nla_data(nla);
  77. memset(msg, 0, al);
  78. atomic_set(&data->dm_hit_count, dm_hit_limit);
  79. }
  80. static void send_dm_alert(struct work_struct *unused)
  81. {
  82. struct sk_buff *skb;
  83. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  84. /*
  85. * Grab the skb we're about to send
  86. */
  87. skb = data->skb;
  88. /*
  89. * Replace it with a new one
  90. */
  91. reset_per_cpu_data(data);
  92. /*
  93. * Ship it!
  94. */
  95. genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
  96. }
  97. /*
  98. * This is the timer function to delay the sending of an alert
  99. * in the event that more drops will arrive during the
  100. * hysteresis period. Note that it operates under the timer interrupt
  101. * so we don't need to disable preemption here
  102. */
  103. static void sched_send_work(unsigned long unused)
  104. {
  105. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  106. schedule_work(&data->dm_alert_work);
  107. }
  108. static void trace_drop_common(struct sk_buff *skb, void *location)
  109. {
  110. struct net_dm_alert_msg *msg;
  111. struct nlmsghdr *nlh;
  112. struct nlattr *nla;
  113. int i;
  114. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  115. if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
  116. /*
  117. * we're already at zero, discard this hit
  118. */
  119. goto out;
  120. }
  121. nlh = (struct nlmsghdr *)data->skb->data;
  122. nla = genlmsg_data(nlmsg_data(nlh));
  123. msg = nla_data(nla);
  124. for (i = 0; i < msg->entries; i++) {
  125. if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
  126. msg->points[i].count++;
  127. atomic_inc(&data->dm_hit_count);
  128. goto out;
  129. }
  130. }
  131. /*
  132. * We need to create a new entry
  133. */
  134. __nla_reserve_nohdr(data->skb, sizeof(struct net_dm_drop_point));
  135. nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
  136. memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
  137. msg->points[msg->entries].count = 1;
  138. msg->entries++;
  139. if (!timer_pending(&data->send_timer)) {
  140. data->send_timer.expires = jiffies + dm_delay * HZ;
  141. add_timer_on(&data->send_timer, smp_processor_id());
  142. }
  143. out:
  144. return;
  145. }
  146. static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
  147. {
  148. trace_drop_common(skb, location);
  149. }
  150. static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi)
  151. {
  152. struct dm_hw_stat_delta *new_stat;
  153. /*
  154. * Don't check napi structures with no associated device
  155. */
  156. if (!napi->dev)
  157. return;
  158. rcu_read_lock();
  159. list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
  160. /*
  161. * only add a note to our monitor buffer if:
  162. * 1) this is the dev we received on
  163. * 2) its after the last_rx delta
  164. * 3) our rx_dropped count has gone up
  165. */
  166. if ((new_stat->dev == napi->dev) &&
  167. (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
  168. (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
  169. trace_drop_common(NULL, NULL);
  170. new_stat->last_drop_val = napi->dev->stats.rx_dropped;
  171. new_stat->last_rx = jiffies;
  172. break;
  173. }
  174. }
  175. rcu_read_unlock();
  176. }
  177. static int set_all_monitor_traces(int state)
  178. {
  179. int rc = 0;
  180. struct dm_hw_stat_delta *new_stat = NULL;
  181. struct dm_hw_stat_delta *temp;
  182. spin_lock(&trace_state_lock);
  183. if (state == trace_state) {
  184. rc = -EAGAIN;
  185. goto out_unlock;
  186. }
  187. switch (state) {
  188. case TRACE_ON:
  189. rc |= register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
  190. rc |= register_trace_napi_poll(trace_napi_poll_hit, NULL);
  191. break;
  192. case TRACE_OFF:
  193. rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
  194. rc |= unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
  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. kfree_rcu(new_stat, rcu);
  203. }
  204. }
  205. break;
  206. default:
  207. rc = 1;
  208. break;
  209. }
  210. if (!rc)
  211. trace_state = state;
  212. else
  213. rc = -EINPROGRESS;
  214. out_unlock:
  215. spin_unlock(&trace_state_lock);
  216. return rc;
  217. }
  218. static int net_dm_cmd_config(struct sk_buff *skb,
  219. struct genl_info *info)
  220. {
  221. return -ENOTSUPP;
  222. }
  223. static int net_dm_cmd_trace(struct sk_buff *skb,
  224. struct genl_info *info)
  225. {
  226. switch (info->genlhdr->cmd) {
  227. case NET_DM_CMD_START:
  228. return set_all_monitor_traces(TRACE_ON);
  229. break;
  230. case NET_DM_CMD_STOP:
  231. return set_all_monitor_traces(TRACE_OFF);
  232. break;
  233. }
  234. return -ENOTSUPP;
  235. }
  236. static int dropmon_net_event(struct notifier_block *ev_block,
  237. unsigned long event, void *ptr)
  238. {
  239. struct net_device *dev = ptr;
  240. struct dm_hw_stat_delta *new_stat = NULL;
  241. struct dm_hw_stat_delta *tmp;
  242. switch (event) {
  243. case NETDEV_REGISTER:
  244. new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
  245. if (!new_stat)
  246. goto out;
  247. new_stat->dev = dev;
  248. new_stat->last_rx = jiffies;
  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. kfree_rcu(new_stat, rcu);
  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. struct per_cpu_dm_data *data;
  291. int cpu, rc;
  292. printk(KERN_INFO "Initializing network drop monitor service\n");
  293. if (sizeof(void *) > 8) {
  294. printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
  295. return -ENOSPC;
  296. }
  297. rc = genl_register_family_with_ops(&net_drop_monitor_family,
  298. dropmon_ops,
  299. ARRAY_SIZE(dropmon_ops));
  300. if (rc) {
  301. printk(KERN_ERR "Could not create drop monitor netlink family\n");
  302. return rc;
  303. }
  304. rc = register_netdevice_notifier(&dropmon_net_notifier);
  305. if (rc < 0) {
  306. printk(KERN_CRIT "Failed to register netdevice notifier\n");
  307. goto out_unreg;
  308. }
  309. rc = 0;
  310. for_each_present_cpu(cpu) {
  311. data = &per_cpu(dm_cpu_data, cpu);
  312. reset_per_cpu_data(data);
  313. INIT_WORK(&data->dm_alert_work, send_dm_alert);
  314. init_timer(&data->send_timer);
  315. data->send_timer.data = cpu;
  316. data->send_timer.function = sched_send_work;
  317. }
  318. goto out;
  319. out_unreg:
  320. genl_unregister_family(&net_drop_monitor_family);
  321. out:
  322. return rc;
  323. }
  324. late_initcall(init_net_drop_monitor);