drop_monitor.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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_MUTEX(trace_state_mutex);
  39. struct per_cpu_dm_data {
  40. struct work_struct dm_alert_work;
  41. struct sk_buff __rcu *skb;
  42. atomic_t dm_hit_count;
  43. struct timer_list send_timer;
  44. int cpu;
  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. .id = GENL_ID_GENERATE,
  55. .hdrsize = 0,
  56. .name = "NET_DM",
  57. .version = 2,
  58. .maxattr = NET_DM_CMD_MAX,
  59. };
  60. static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
  61. static int dm_hit_limit = 64;
  62. static int dm_delay = 1;
  63. static unsigned long dm_hw_check_delta = 2*HZ;
  64. static LIST_HEAD(hw_stats_list);
  65. static void reset_per_cpu_data(struct per_cpu_dm_data *data)
  66. {
  67. size_t al;
  68. struct net_dm_alert_msg *msg;
  69. struct nlattr *nla;
  70. struct sk_buff *skb;
  71. struct sk_buff *oskb = rcu_dereference_protected(data->skb, 1);
  72. al = sizeof(struct net_dm_alert_msg);
  73. al += dm_hit_limit * sizeof(struct net_dm_drop_point);
  74. al += sizeof(struct nlattr);
  75. skb = genlmsg_new(al, GFP_KERNEL);
  76. if (skb) {
  77. genlmsg_put(skb, 0, 0, &net_drop_monitor_family,
  78. 0, NET_DM_CMD_ALERT);
  79. nla = nla_reserve(skb, NLA_UNSPEC,
  80. sizeof(struct net_dm_alert_msg));
  81. msg = nla_data(nla);
  82. memset(msg, 0, al);
  83. } else
  84. schedule_work_on(data->cpu, &data->dm_alert_work);
  85. /*
  86. * Don't need to lock this, since we are guaranteed to only
  87. * run this on a single cpu at a time.
  88. * Note also that we only update data->skb if the old and new skb
  89. * pointers don't match. This ensures that we don't continually call
  90. * synchornize_rcu if we repeatedly fail to alloc a new netlink message.
  91. */
  92. if (skb != oskb) {
  93. rcu_assign_pointer(data->skb, skb);
  94. synchronize_rcu();
  95. atomic_set(&data->dm_hit_count, dm_hit_limit);
  96. }
  97. }
  98. static void send_dm_alert(struct work_struct *unused)
  99. {
  100. struct sk_buff *skb;
  101. struct per_cpu_dm_data *data = &get_cpu_var(dm_cpu_data);
  102. WARN_ON_ONCE(data->cpu != smp_processor_id());
  103. /*
  104. * Grab the skb we're about to send
  105. */
  106. skb = rcu_dereference_protected(data->skb, 1);
  107. /*
  108. * Replace it with a new one
  109. */
  110. reset_per_cpu_data(data);
  111. /*
  112. * Ship it!
  113. */
  114. if (skb)
  115. genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
  116. put_cpu_var(dm_cpu_data);
  117. }
  118. /*
  119. * This is the timer function to delay the sending of an alert
  120. * in the event that more drops will arrive during the
  121. * hysteresis period. Note that it operates under the timer interrupt
  122. * so we don't need to disable preemption here
  123. */
  124. static void sched_send_work(unsigned long unused)
  125. {
  126. struct per_cpu_dm_data *data = &get_cpu_var(dm_cpu_data);
  127. schedule_work_on(smp_processor_id(), &data->dm_alert_work);
  128. put_cpu_var(dm_cpu_data);
  129. }
  130. static void trace_drop_common(struct sk_buff *skb, void *location)
  131. {
  132. struct net_dm_alert_msg *msg;
  133. struct nlmsghdr *nlh;
  134. struct nlattr *nla;
  135. int i;
  136. struct sk_buff *dskb;
  137. struct per_cpu_dm_data *data = &get_cpu_var(dm_cpu_data);
  138. rcu_read_lock();
  139. dskb = rcu_dereference(data->skb);
  140. if (!dskb)
  141. goto out;
  142. if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
  143. /*
  144. * we're already at zero, discard this hit
  145. */
  146. goto out;
  147. }
  148. nlh = (struct nlmsghdr *)dskb->data;
  149. nla = genlmsg_data(nlmsg_data(nlh));
  150. msg = nla_data(nla);
  151. for (i = 0; i < msg->entries; i++) {
  152. if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
  153. msg->points[i].count++;
  154. atomic_inc(&data->dm_hit_count);
  155. goto out;
  156. }
  157. }
  158. /*
  159. * We need to create a new entry
  160. */
  161. __nla_reserve_nohdr(dskb, sizeof(struct net_dm_drop_point));
  162. nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
  163. memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
  164. msg->points[msg->entries].count = 1;
  165. msg->entries++;
  166. if (!timer_pending(&data->send_timer)) {
  167. data->send_timer.expires = jiffies + dm_delay * HZ;
  168. add_timer_on(&data->send_timer, smp_processor_id());
  169. }
  170. out:
  171. rcu_read_unlock();
  172. put_cpu_var(dm_cpu_data);
  173. return;
  174. }
  175. static void trace_kfree_skb_hit(void *ignore, struct sk_buff *skb, void *location)
  176. {
  177. trace_drop_common(skb, location);
  178. }
  179. static void trace_napi_poll_hit(void *ignore, struct napi_struct *napi)
  180. {
  181. struct dm_hw_stat_delta *new_stat;
  182. /*
  183. * Don't check napi structures with no associated device
  184. */
  185. if (!napi->dev)
  186. return;
  187. rcu_read_lock();
  188. list_for_each_entry_rcu(new_stat, &hw_stats_list, list) {
  189. /*
  190. * only add a note to our monitor buffer if:
  191. * 1) this is the dev we received on
  192. * 2) its after the last_rx delta
  193. * 3) our rx_dropped count has gone up
  194. */
  195. if ((new_stat->dev == napi->dev) &&
  196. (time_after(jiffies, new_stat->last_rx + dm_hw_check_delta)) &&
  197. (napi->dev->stats.rx_dropped != new_stat->last_drop_val)) {
  198. trace_drop_common(NULL, NULL);
  199. new_stat->last_drop_val = napi->dev->stats.rx_dropped;
  200. new_stat->last_rx = jiffies;
  201. break;
  202. }
  203. }
  204. rcu_read_unlock();
  205. }
  206. static int set_all_monitor_traces(int state)
  207. {
  208. int rc = 0;
  209. struct dm_hw_stat_delta *new_stat = NULL;
  210. struct dm_hw_stat_delta *temp;
  211. mutex_lock(&trace_state_mutex);
  212. if (state == trace_state) {
  213. rc = -EAGAIN;
  214. goto out_unlock;
  215. }
  216. switch (state) {
  217. case TRACE_ON:
  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. break;
  235. default:
  236. rc = 1;
  237. break;
  238. }
  239. if (!rc)
  240. trace_state = state;
  241. else
  242. rc = -EINPROGRESS;
  243. out_unlock:
  244. mutex_unlock(&trace_state_mutex);
  245. return rc;
  246. }
  247. static int net_dm_cmd_config(struct sk_buff *skb,
  248. struct genl_info *info)
  249. {
  250. return -ENOTSUPP;
  251. }
  252. static int net_dm_cmd_trace(struct sk_buff *skb,
  253. struct genl_info *info)
  254. {
  255. switch (info->genlhdr->cmd) {
  256. case NET_DM_CMD_START:
  257. return set_all_monitor_traces(TRACE_ON);
  258. break;
  259. case NET_DM_CMD_STOP:
  260. return set_all_monitor_traces(TRACE_OFF);
  261. break;
  262. }
  263. return -ENOTSUPP;
  264. }
  265. static int dropmon_net_event(struct notifier_block *ev_block,
  266. unsigned long event, void *ptr)
  267. {
  268. struct net_device *dev = ptr;
  269. struct dm_hw_stat_delta *new_stat = NULL;
  270. struct dm_hw_stat_delta *tmp;
  271. switch (event) {
  272. case NETDEV_REGISTER:
  273. new_stat = kzalloc(sizeof(struct dm_hw_stat_delta), GFP_KERNEL);
  274. if (!new_stat)
  275. goto out;
  276. new_stat->dev = dev;
  277. new_stat->last_rx = jiffies;
  278. mutex_lock(&trace_state_mutex);
  279. list_add_rcu(&new_stat->list, &hw_stats_list);
  280. mutex_unlock(&trace_state_mutex);
  281. break;
  282. case NETDEV_UNREGISTER:
  283. mutex_lock(&trace_state_mutex);
  284. list_for_each_entry_safe(new_stat, tmp, &hw_stats_list, list) {
  285. if (new_stat->dev == dev) {
  286. new_stat->dev = NULL;
  287. if (trace_state == TRACE_OFF) {
  288. list_del_rcu(&new_stat->list);
  289. kfree_rcu(new_stat, rcu);
  290. break;
  291. }
  292. }
  293. }
  294. mutex_unlock(&trace_state_mutex);
  295. break;
  296. }
  297. out:
  298. return NOTIFY_DONE;
  299. }
  300. static struct genl_ops dropmon_ops[] = {
  301. {
  302. .cmd = NET_DM_CMD_CONFIG,
  303. .doit = net_dm_cmd_config,
  304. },
  305. {
  306. .cmd = NET_DM_CMD_START,
  307. .doit = net_dm_cmd_trace,
  308. },
  309. {
  310. .cmd = NET_DM_CMD_STOP,
  311. .doit = net_dm_cmd_trace,
  312. },
  313. };
  314. static struct notifier_block dropmon_net_notifier = {
  315. .notifier_call = dropmon_net_event
  316. };
  317. static int __init init_net_drop_monitor(void)
  318. {
  319. struct per_cpu_dm_data *data;
  320. int cpu, rc;
  321. printk(KERN_INFO "Initializing network drop monitor service\n");
  322. if (sizeof(void *) > 8) {
  323. printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
  324. return -ENOSPC;
  325. }
  326. rc = genl_register_family_with_ops(&net_drop_monitor_family,
  327. dropmon_ops,
  328. ARRAY_SIZE(dropmon_ops));
  329. if (rc) {
  330. printk(KERN_ERR "Could not create drop monitor netlink family\n");
  331. return rc;
  332. }
  333. rc = register_netdevice_notifier(&dropmon_net_notifier);
  334. if (rc < 0) {
  335. printk(KERN_CRIT "Failed to register netdevice notifier\n");
  336. goto out_unreg;
  337. }
  338. rc = 0;
  339. for_each_present_cpu(cpu) {
  340. data = &per_cpu(dm_cpu_data, cpu);
  341. data->cpu = cpu;
  342. INIT_WORK(&data->dm_alert_work, send_dm_alert);
  343. init_timer(&data->send_timer);
  344. data->send_timer.data = cpu;
  345. data->send_timer.function = sched_send_work;
  346. reset_per_cpu_data(data);
  347. }
  348. goto out;
  349. out_unreg:
  350. genl_unregister_family(&net_drop_monitor_family);
  351. out:
  352. return rc;
  353. }
  354. late_initcall(init_net_drop_monitor);