drop_monitor.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <trace/skb.h>
  25. #include <asm/unaligned.h>
  26. #define TRACE_ON 1
  27. #define TRACE_OFF 0
  28. static void send_dm_alert(struct work_struct *unused);
  29. /*
  30. * Globals, our netlink socket pointer
  31. * and the work handle that will send up
  32. * netlink alerts
  33. */
  34. struct sock *dm_sock;
  35. struct per_cpu_dm_data {
  36. struct work_struct dm_alert_work;
  37. struct sk_buff *skb;
  38. atomic_t dm_hit_count;
  39. struct timer_list send_timer;
  40. };
  41. static struct genl_family net_drop_monitor_family = {
  42. .id = GENL_ID_GENERATE,
  43. .hdrsize = 0,
  44. .name = "NET_DM",
  45. .version = 2,
  46. .maxattr = NET_DM_CMD_MAX,
  47. };
  48. static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
  49. static int dm_hit_limit = 64;
  50. static int dm_delay = 1;
  51. static void reset_per_cpu_data(struct per_cpu_dm_data *data)
  52. {
  53. size_t al;
  54. struct net_dm_alert_msg *msg;
  55. struct nlattr *nla;
  56. al = sizeof(struct net_dm_alert_msg);
  57. al += dm_hit_limit * sizeof(struct net_dm_drop_point);
  58. al += sizeof(struct nlattr);
  59. data->skb = genlmsg_new(al, GFP_KERNEL);
  60. genlmsg_put(data->skb, 0, 0, &net_drop_monitor_family,
  61. 0, NET_DM_CMD_ALERT);
  62. nla = nla_reserve(data->skb, NLA_UNSPEC, sizeof(struct net_dm_alert_msg));
  63. msg = nla_data(nla);
  64. memset(msg, 0, al);
  65. atomic_set(&data->dm_hit_count, dm_hit_limit);
  66. }
  67. static void send_dm_alert(struct work_struct *unused)
  68. {
  69. struct sk_buff *skb;
  70. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  71. /*
  72. * Grab the skb we're about to send
  73. */
  74. skb = data->skb;
  75. /*
  76. * Replace it with a new one
  77. */
  78. reset_per_cpu_data(data);
  79. /*
  80. * Ship it!
  81. */
  82. genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL);
  83. }
  84. /*
  85. * This is the timer function to delay the sending of an alert
  86. * in the event that more drops will arrive during the
  87. * hysteresis period. Note that it operates under the timer interrupt
  88. * so we don't need to disable preemption here
  89. */
  90. static void sched_send_work(unsigned long unused)
  91. {
  92. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  93. schedule_work(&data->dm_alert_work);
  94. }
  95. static void trace_kfree_skb_hit(struct sk_buff *skb, void *location)
  96. {
  97. struct net_dm_alert_msg *msg;
  98. struct nlmsghdr *nlh;
  99. struct nlattr *nla;
  100. int i;
  101. struct per_cpu_dm_data *data = &__get_cpu_var(dm_cpu_data);
  102. if (!atomic_add_unless(&data->dm_hit_count, -1, 0)) {
  103. /*
  104. * we're already at zero, discard this hit
  105. */
  106. goto out;
  107. }
  108. nlh = (struct nlmsghdr *)data->skb->data;
  109. nla = genlmsg_data(nlmsg_data(nlh));
  110. msg = nla_data(nla);
  111. for (i = 0; i < msg->entries; i++) {
  112. if (!memcmp(&location, msg->points[i].pc, sizeof(void *))) {
  113. msg->points[i].count++;
  114. goto out;
  115. }
  116. }
  117. /*
  118. * We need to create a new entry
  119. */
  120. __nla_reserve_nohdr(data->skb, sizeof(struct net_dm_drop_point));
  121. nla->nla_len += NLA_ALIGN(sizeof(struct net_dm_drop_point));
  122. memcpy(msg->points[msg->entries].pc, &location, sizeof(void *));
  123. msg->points[msg->entries].count = 1;
  124. msg->entries++;
  125. if (!timer_pending(&data->send_timer)) {
  126. data->send_timer.expires = jiffies + dm_delay * HZ;
  127. add_timer_on(&data->send_timer, smp_processor_id());
  128. }
  129. out:
  130. return;
  131. }
  132. static int set_all_monitor_traces(int state)
  133. {
  134. int rc = 0;
  135. switch (state) {
  136. case TRACE_ON:
  137. rc |= register_trace_kfree_skb(trace_kfree_skb_hit);
  138. break;
  139. case TRACE_OFF:
  140. rc |= unregister_trace_kfree_skb(trace_kfree_skb_hit);
  141. tracepoint_synchronize_unregister();
  142. break;
  143. default:
  144. rc = 1;
  145. break;
  146. }
  147. if (rc)
  148. return -EINPROGRESS;
  149. return rc;
  150. }
  151. static int net_dm_cmd_config(struct sk_buff *skb,
  152. struct genl_info *info)
  153. {
  154. return -ENOTSUPP;
  155. }
  156. static int net_dm_cmd_trace(struct sk_buff *skb,
  157. struct genl_info *info)
  158. {
  159. switch (info->genlhdr->cmd) {
  160. case NET_DM_CMD_START:
  161. return set_all_monitor_traces(TRACE_ON);
  162. break;
  163. case NET_DM_CMD_STOP:
  164. return set_all_monitor_traces(TRACE_OFF);
  165. break;
  166. }
  167. return -ENOTSUPP;
  168. }
  169. static struct genl_ops dropmon_ops[] = {
  170. {
  171. .cmd = NET_DM_CMD_CONFIG,
  172. .doit = net_dm_cmd_config,
  173. },
  174. {
  175. .cmd = NET_DM_CMD_START,
  176. .doit = net_dm_cmd_trace,
  177. },
  178. {
  179. .cmd = NET_DM_CMD_STOP,
  180. .doit = net_dm_cmd_trace,
  181. },
  182. };
  183. static int __init init_net_drop_monitor(void)
  184. {
  185. int cpu;
  186. int rc, i, ret;
  187. struct per_cpu_dm_data *data;
  188. printk(KERN_INFO "Initalizing network drop monitor service\n");
  189. if (sizeof(void *) > 8) {
  190. printk(KERN_ERR "Unable to store program counters on this arch, Drop monitor failed\n");
  191. return -ENOSPC;
  192. }
  193. if (genl_register_family(&net_drop_monitor_family) < 0) {
  194. printk(KERN_ERR "Could not create drop monitor netlink family\n");
  195. return -EFAULT;
  196. }
  197. rc = -EFAULT;
  198. for (i = 0; i < ARRAY_SIZE(dropmon_ops); i++) {
  199. ret = genl_register_ops(&net_drop_monitor_family,
  200. &dropmon_ops[i]);
  201. if (ret) {
  202. printk(KERN_CRIT "failed to register operation %d\n",
  203. dropmon_ops[i].cmd);
  204. goto out_unreg;
  205. }
  206. }
  207. rc = 0;
  208. for_each_present_cpu(cpu) {
  209. data = &per_cpu(dm_cpu_data, cpu);
  210. reset_per_cpu_data(data);
  211. INIT_WORK(&data->dm_alert_work, send_dm_alert);
  212. init_timer(&data->send_timer);
  213. data->send_timer.data = cpu;
  214. data->send_timer.function = sched_send_work;
  215. }
  216. goto out;
  217. out_unreg:
  218. genl_unregister_family(&net_drop_monitor_family);
  219. out:
  220. return rc;
  221. }
  222. late_initcall(init_net_drop_monitor);