drop_monitor.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. /*
  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 DEFINE_MUTEX(trace_state_mutex);
  38. struct per_cpu_dm_data {
  39. spinlock_t lock;
  40. struct sk_buff *skb;
  41. struct work_struct dm_alert_work;
  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. };
  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 struct sk_buff *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. struct sk_buff *skb;
  68. unsigned long flags;
  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. skb = genlmsg_new(al, GFP_KERNEL);
  73. if (skb) {
  74. genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
  75. 0, NET_DM_CMD_ALERT);
  76. nla = nla_reserve(skb, NLA_UNSPEC,
  77. sizeof(struct net_dm_alert_msg));
  78. msg = nla_data(nla);
  79. memset(msg, 0, al);
  80. } else {
  81. mod_timer(&data->send_timer, jiffies + HZ / 10);
  82. }
  83. spin_lock_irqsave(&data->lock, flags);
  84. swap(data->skb, skb);
  85. spin_unlock_irqrestore(&data->lock, flags);
  86. return skb;
  87. }
  88. static void send_dm_alert(struct work_struct *work)
  89. {
  90. struct sk_buff *skb;
  91. struct per_cpu_dm_data *data;
  92. data = container_of(work, struct per_cpu_dm_data, dm_alert_work);
  93. skb = reset_per_cpu_data(data);
  94. if (skb)
  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.
  101. */
  102. static void sched_send_work(unsigned long _data)
  103. {
  104. struct per_cpu_dm_data *data = (struct per_cpu_dm_data *)_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 sk_buff *dskb;
  114. struct per_cpu_dm_data *data;
  115. unsigned long flags;
  116. local_irq_save(flags);
  117. data = &__get_cpu_var(dm_cpu_data);
  118. spin_lock(&data->lock);
  119. dskb = data->skb;
  120. if (!dskb)
  121. goto out;
  122. nlh = (struct nlmsghdr *)dskb->data;
  123. nla = genlmsg_data(nlmsg_data(nlh));
  124. msg = nla_data(nla);
  125. for (i = 0; i < msg->entries; i++) {
  126. if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
  127. msg->points[i].count++;
  128. goto out;
  129. }
  130. }
  131. if (msg->entries == dm_hit_limit)
  132. goto out;
  133. /*
  134. * We need to create a new entry
  135. */
  136. __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
  137. nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
  138. memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
  139. msg->points[msg->entries].count = 1;
  140. msg->entries++;
  141. if (!timer_pending(&data->send_timer)) {
  142. data->send_timer.expires = jiffies + dm_delay * HZ;
  143. add_timer(&data->send_timer);
  144. }
  145. out:
  146. spin_unlock_irqrestore(&data->lock, flags);
  147. }
  148. static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
  149. {
  150. trace_drop_common(skb, location);
  151. }
  152. static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi)
  153. {
  154. struct dm_hw_stat_delta *new_stat;
  155. /*
  156. * Don't check napi structures with no associated device
  157. */
  158. if (!napi->dev)
  159. return;
  160. rcu_read_lock();
  161. list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
  162. /*
  163. * only add a note to our monitor buffer if:
  164. * 1) this is the dev we received on
  165. * 2) its after the last_rx delta
  166. * 3) our rx_dropped count has gone up
  167. */
  168. if ((new_stat->dev == napi->dev) &&
  169. (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
  170. (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
  171. trace_drop_common(NULL, NULL);
  172. new_stat->last_drop_val = napi->dev->stats.rx_dropped;
  173. new_stat->last_rx = jiffies;
  174. break;
  175. }
  176. }
  177. rcu_read_unlock();
  178. }
  179. static int set_all_monitor_traces(int state)
  180. {
  181. int rc = 0;
  182. struct dm_hw_stat_delta *new_stat = NULL;
  183. struct dm_hw_stat_delta *temp;
  184. mutex_lock(&trace_state_mutex);
  185. if (state == trace_state) {
  186. rc = -EAGAIN;
  187. goto out_unlock;
  188. }
  189. switch (state) {
  190. case TRACE_ON:
  191. rc |= register_trace_kfree_skb(trace_kfree_skb_hit, NULL);
  192. rc |= register_trace_napi_poll(trace_napi_poll_hit, NULL);
  193. break;
  194. case TRACE_OFF:
  195. rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit, NULL);
  196. rc |= unregister_trace_napi_poll(trace_napi_poll_hit, NULL);
  197. tracepoint_synchronize_unregister();
  198. /*
  199. * Clean the device list
  200. */
  201. list_for_each_entry_safe(new_stat, temp, &hw_stats_list, list) {
  202. if (new_stat->dev == NULL) {
  203. list_del_rcu(&new_stat->list);
  204. kfree_rcu(new_stat, rcu);
  205. }
  206. }
  207. break;
  208. default:
  209. rc = 1;
  210. break;
  211. }
  212. if (!rc)
  213. trace_state = state;
  214. else
  215. rc = -EINPROGRESS;
  216. out_unlock:
  217. mutex_unlock(&trace_state_mutex);
  218. return rc;
  219. }
  220. static int net_dm_cmd_config(struct sk_buff *skb,
  221. struct genl_info *info)
  222. {
  223. return -ENOTSUPP;
  224. }
  225. static int net_dm_cmd_trace(struct sk_buff *skb,
  226. struct genl_info *info)
  227. {
  228. switch (info->genlhdr->cmd) {
  229. case NET_DM_CMD_START:
  230. return set_all_monitor_traces(TRACE_ON);
  231. break;
  232. case NET_DM_CMD_STOP:
  233. return set_all_monitor_traces(TRACE_OFF);
  234. break;
  235. }
  236. return -ENOTSUPP;
  237. }
  238. static int dropmon_net_event(struct notifier_block *ev_block,
  239. unsigned long event, void *ptr)
  240. {
  241. struct net_device *dev = ptr;
  242. struct dm_hw_stat_delta *new_stat = NULL;
  243. struct dm_hw_stat_delta *tmp;
  244. switch (event) {
  245. case NETDEV_REGISTER:
  246. new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
  247. if (!new_stat)
  248. goto out;
  249. new_stat->dev = dev;
  250. new_stat->last_rx = jiffies;
  251. mutex_lock(&trace_state_mutex);
  252. list_add_rcu(&new_stat->list, &hw_stats_list);
  253. mutex_unlock(&trace_state_mutex);
  254. break;
  255. case NETDEV_UNREGISTER:
  256. mutex_lock(&trace_state_mutex);
  257. list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
  258. if (new_stat->dev == dev) {
  259. new_stat->dev = NULL;
  260. if (trace_state == TRACE_OFF) {
  261. list_del_rcu(&new_stat->list);
  262. kfree_rcu(new_stat, rcu);
  263. break;
  264. }
  265. }
  266. }
  267. mutex_unlock(&trace_state_mutex);
  268. break;
  269. }
  270. out:
  271. return NOTIFY_DONE;
  272. }
  273. static struct genl_ops dropmon_ops[] = {
  274. {
  275. .cmd = NET_DM_CMD_CONFIG,
  276. .doit = net_dm_cmd_config,
  277. },
  278. {
  279. .cmd = NET_DM_CMD_START,
  280. .doit = net_dm_cmd_trace,
  281. },
  282. {
  283. .cmd = NET_DM_CMD_STOP,
  284. .doit = net_dm_cmd_trace,
  285. },
  286. };
  287. static struct notifier_block dropmon_net_notifier = {
  288. .notifier_call = dropmon_net_event
  289. };
  290. static int __init init_net_drop_monitor(void)
  291. {
  292. struct per_cpu_dm_data *data;
  293. int cpu, rc;
  294. printk(KERN_INFO "Initializing network drop monitor service\n");
  295. if (sizeof(void *) > 8) {
  296. printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
  297. return -ENOSPC;
  298. }
  299. rc = genl_register_family_with_ops(&net_drop_monitor_family,
  300. dropmon_ops,
  301. ARRAY_SIZE(dropmon_ops));
  302. if (rc) {
  303. printk(KERN_ERR "Could not create drop monitor netlink family\n");
  304. return rc;
  305. }
  306. rc = register_netdevice_notifier(&dropmon_net_notifier);
  307. if (rc < 0) {
  308. printk(KERN_CRIT "Failed to register netdevice notifier\n");
  309. goto out_unreg;
  310. }
  311. rc = 0;
  312. for_each_present_cpu(cpu) {
  313. data = &per_cpu(dm_cpu_data, cpu);
  314. INIT_WORK(&data->dm_alert_work, send_dm_alert);
  315. init_timer(&data->send_timer);
  316. data->send_timer.data = (unsigned long)data;
  317. data->send_timer.function = sched_send_work;
  318. spin_lock_init(&data->lock);
  319. reset_per_cpu_data(data);
  320. }
  321. goto out;
  322. out_unreg:
  323. genl_unregister_family(&net_drop_monitor_family);
  324. out:
  325. return rc;
  326. }
  327. late_initcall(init_net_drop_monitor);