drop_monitor.c 5.5 KB

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