flow.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* flow.c: Generic flow cache.
  2. *
  3. * Copyright (C) 2003 Alexey N. Kuznetsov (kuznet@ms2.inr.ac.ru)
  4. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/list.h>
  9. #include <linux/jhash.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/mm.h>
  12. #include <linux/random.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/smp.h>
  16. #include <linux/completion.h>
  17. #include <linux/percpu.h>
  18. #include <linux/bitops.h>
  19. #include <linux/notifier.h>
  20. #include <linux/cpu.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/mutex.h>
  23. #include <net/flow.h>
  24. #include <asm/atomic.h>
  25. #include <asm/semaphore.h>
  26. #include <linux/security.h>
  27. struct flow_cache_entry {
  28. struct flow_cache_entry *next;
  29. u16 family;
  30. u8 dir;
  31. struct flowi key;
  32. u32 genid;
  33. u32 sk_sid;
  34. void *object;
  35. atomic_t *object_ref;
  36. };
  37. atomic_t flow_cache_genid = ATOMIC_INIT(0);
  38. static u32 flow_hash_shift;
  39. #define flow_hash_size (1 << flow_hash_shift)
  40. static DEFINE_PER_CPU(struct flow_cache_entry **, flow_tables) = { NULL };
  41. #define flow_table(cpu) (per_cpu(flow_tables, cpu))
  42. static kmem_cache_t *flow_cachep __read_mostly;
  43. static int flow_lwm, flow_hwm;
  44. struct flow_percpu_info {
  45. int hash_rnd_recalc;
  46. u32 hash_rnd;
  47. int count;
  48. } ____cacheline_aligned;
  49. static DEFINE_PER_CPU(struct flow_percpu_info, flow_hash_info) = { 0 };
  50. #define flow_hash_rnd_recalc(cpu) \
  51. (per_cpu(flow_hash_info, cpu).hash_rnd_recalc)
  52. #define flow_hash_rnd(cpu) \
  53. (per_cpu(flow_hash_info, cpu).hash_rnd)
  54. #define flow_count(cpu) \
  55. (per_cpu(flow_hash_info, cpu).count)
  56. static struct timer_list flow_hash_rnd_timer;
  57. #define FLOW_HASH_RND_PERIOD (10 * 60 * HZ)
  58. struct flow_flush_info {
  59. atomic_t cpuleft;
  60. struct completion completion;
  61. };
  62. static DEFINE_PER_CPU(struct tasklet_struct, flow_flush_tasklets) = { NULL };
  63. #define flow_flush_tasklet(cpu) (&per_cpu(flow_flush_tasklets, cpu))
  64. static void flow_cache_new_hashrnd(unsigned long arg)
  65. {
  66. int i;
  67. for_each_possible_cpu(i)
  68. flow_hash_rnd_recalc(i) = 1;
  69. flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
  70. add_timer(&flow_hash_rnd_timer);
  71. }
  72. static void __flow_cache_shrink(int cpu, int shrink_to)
  73. {
  74. struct flow_cache_entry *fle, **flp;
  75. int i;
  76. for (i = 0; i < flow_hash_size; i++) {
  77. int k = 0;
  78. flp = &flow_table(cpu)[i];
  79. while ((fle = *flp) != NULL && k < shrink_to) {
  80. k++;
  81. flp = &fle->next;
  82. }
  83. while ((fle = *flp) != NULL) {
  84. *flp = fle->next;
  85. if (fle->object)
  86. atomic_dec(fle->object_ref);
  87. kmem_cache_free(flow_cachep, fle);
  88. flow_count(cpu)--;
  89. }
  90. }
  91. }
  92. static void flow_cache_shrink(int cpu)
  93. {
  94. int shrink_to = flow_lwm / flow_hash_size;
  95. __flow_cache_shrink(cpu, shrink_to);
  96. }
  97. static void flow_new_hash_rnd(int cpu)
  98. {
  99. get_random_bytes(&flow_hash_rnd(cpu), sizeof(u32));
  100. flow_hash_rnd_recalc(cpu) = 0;
  101. __flow_cache_shrink(cpu, 0);
  102. }
  103. static u32 flow_hash_code(struct flowi *key, int cpu)
  104. {
  105. u32 *k = (u32 *) key;
  106. return (jhash2(k, (sizeof(*key) / sizeof(u32)), flow_hash_rnd(cpu)) &
  107. (flow_hash_size - 1));
  108. }
  109. #if (BITS_PER_LONG == 64)
  110. typedef u64 flow_compare_t;
  111. #else
  112. typedef u32 flow_compare_t;
  113. #endif
  114. extern void flowi_is_missized(void);
  115. /* I hear what you're saying, use memcmp. But memcmp cannot make
  116. * important assumptions that we can here, such as alignment and
  117. * constant size.
  118. */
  119. static int flow_key_compare(struct flowi *key1, struct flowi *key2)
  120. {
  121. flow_compare_t *k1, *k1_lim, *k2;
  122. const int n_elem = sizeof(struct flowi) / sizeof(flow_compare_t);
  123. if (sizeof(struct flowi) % sizeof(flow_compare_t))
  124. flowi_is_missized();
  125. k1 = (flow_compare_t *) key1;
  126. k1_lim = k1 + n_elem;
  127. k2 = (flow_compare_t *) key2;
  128. do {
  129. if (*k1++ != *k2++)
  130. return 1;
  131. } while (k1 < k1_lim);
  132. return 0;
  133. }
  134. void *flow_cache_lookup(struct flowi *key, u32 sk_sid, u16 family, u8 dir,
  135. flow_resolve_t resolver)
  136. {
  137. struct flow_cache_entry *fle, **head;
  138. unsigned int hash;
  139. int cpu;
  140. local_bh_disable();
  141. cpu = smp_processor_id();
  142. fle = NULL;
  143. /* Packet really early in init? Making flow_cache_init a
  144. * pre-smp initcall would solve this. --RR */
  145. if (!flow_table(cpu))
  146. goto nocache;
  147. if (flow_hash_rnd_recalc(cpu))
  148. flow_new_hash_rnd(cpu);
  149. hash = flow_hash_code(key, cpu);
  150. head = &flow_table(cpu)[hash];
  151. for (fle = *head; fle; fle = fle->next) {
  152. if (fle->family == family &&
  153. fle->dir == dir &&
  154. fle->sk_sid == sk_sid &&
  155. flow_key_compare(key, &fle->key) == 0) {
  156. if (fle->genid == atomic_read(&flow_cache_genid)) {
  157. void *ret = fle->object;
  158. if (ret)
  159. atomic_inc(fle->object_ref);
  160. local_bh_enable();
  161. return ret;
  162. }
  163. break;
  164. }
  165. }
  166. if (!fle) {
  167. if (flow_count(cpu) > flow_hwm)
  168. flow_cache_shrink(cpu);
  169. fle = kmem_cache_alloc(flow_cachep, SLAB_ATOMIC);
  170. if (fle) {
  171. fle->next = *head;
  172. *head = fle;
  173. fle->family = family;
  174. fle->dir = dir;
  175. fle->sk_sid = sk_sid;
  176. memcpy(&fle->key, key, sizeof(*key));
  177. fle->object = NULL;
  178. flow_count(cpu)++;
  179. }
  180. }
  181. nocache:
  182. {
  183. void *obj;
  184. atomic_t *obj_ref;
  185. resolver(key, sk_sid, family, dir, &obj, &obj_ref);
  186. if (fle) {
  187. fle->genid = atomic_read(&flow_cache_genid);
  188. if (fle->object)
  189. atomic_dec(fle->object_ref);
  190. fle->object = obj;
  191. fle->object_ref = obj_ref;
  192. if (obj)
  193. atomic_inc(fle->object_ref);
  194. }
  195. local_bh_enable();
  196. return obj;
  197. }
  198. }
  199. static void flow_cache_flush_tasklet(unsigned long data)
  200. {
  201. struct flow_flush_info *info = (void *)data;
  202. int i;
  203. int cpu;
  204. cpu = smp_processor_id();
  205. for (i = 0; i < flow_hash_size; i++) {
  206. struct flow_cache_entry *fle;
  207. fle = flow_table(cpu)[i];
  208. for (; fle; fle = fle->next) {
  209. unsigned genid = atomic_read(&flow_cache_genid);
  210. if (!fle->object || fle->genid == genid)
  211. continue;
  212. fle->object = NULL;
  213. atomic_dec(fle->object_ref);
  214. }
  215. }
  216. if (atomic_dec_and_test(&info->cpuleft))
  217. complete(&info->completion);
  218. }
  219. static void flow_cache_flush_per_cpu(void *) __attribute__((__unused__));
  220. static void flow_cache_flush_per_cpu(void *data)
  221. {
  222. struct flow_flush_info *info = data;
  223. int cpu;
  224. struct tasklet_struct *tasklet;
  225. cpu = smp_processor_id();
  226. tasklet = flow_flush_tasklet(cpu);
  227. tasklet->data = (unsigned long)info;
  228. tasklet_schedule(tasklet);
  229. }
  230. void flow_cache_flush(void)
  231. {
  232. struct flow_flush_info info;
  233. static DEFINE_MUTEX(flow_flush_sem);
  234. /* Don't want cpus going down or up during this. */
  235. lock_cpu_hotplug();
  236. mutex_lock(&flow_flush_sem);
  237. atomic_set(&info.cpuleft, num_online_cpus());
  238. init_completion(&info.completion);
  239. local_bh_disable();
  240. smp_call_function(flow_cache_flush_per_cpu, &info, 1, 0);
  241. flow_cache_flush_tasklet((unsigned long)&info);
  242. local_bh_enable();
  243. wait_for_completion(&info.completion);
  244. mutex_unlock(&flow_flush_sem);
  245. unlock_cpu_hotplug();
  246. }
  247. static void __devinit flow_cache_cpu_prepare(int cpu)
  248. {
  249. struct tasklet_struct *tasklet;
  250. unsigned long order;
  251. for (order = 0;
  252. (PAGE_SIZE << order) <
  253. (sizeof(struct flow_cache_entry *)*flow_hash_size);
  254. order++)
  255. /* NOTHING */;
  256. flow_table(cpu) = (struct flow_cache_entry **)
  257. __get_free_pages(GFP_KERNEL|__GFP_ZERO, order);
  258. if (!flow_table(cpu))
  259. panic("NET: failed to allocate flow cache order %lu\n", order);
  260. flow_hash_rnd_recalc(cpu) = 1;
  261. flow_count(cpu) = 0;
  262. tasklet = flow_flush_tasklet(cpu);
  263. tasklet_init(tasklet, flow_cache_flush_tasklet, 0);
  264. }
  265. #ifdef CONFIG_HOTPLUG_CPU
  266. static int flow_cache_cpu(struct notifier_block *nfb,
  267. unsigned long action,
  268. void *hcpu)
  269. {
  270. if (action == CPU_DEAD)
  271. __flow_cache_shrink((unsigned long)hcpu, 0);
  272. return NOTIFY_OK;
  273. }
  274. #endif /* CONFIG_HOTPLUG_CPU */
  275. static int __init flow_cache_init(void)
  276. {
  277. int i;
  278. flow_cachep = kmem_cache_create("flow_cache",
  279. sizeof(struct flow_cache_entry),
  280. 0, SLAB_HWCACHE_ALIGN,
  281. NULL, NULL);
  282. if (!flow_cachep)
  283. panic("NET: failed to allocate flow cache slab\n");
  284. flow_hash_shift = 10;
  285. flow_lwm = 2 * flow_hash_size;
  286. flow_hwm = 4 * flow_hash_size;
  287. init_timer(&flow_hash_rnd_timer);
  288. flow_hash_rnd_timer.function = flow_cache_new_hashrnd;
  289. flow_hash_rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
  290. add_timer(&flow_hash_rnd_timer);
  291. for_each_possible_cpu(i)
  292. flow_cache_cpu_prepare(i);
  293. hotcpu_notifier(flow_cache_cpu, 0);
  294. return 0;
  295. }
  296. module_init(flow_cache_init);
  297. EXPORT_SYMBOL(flow_cache_genid);
  298. EXPORT_SYMBOL(flow_cache_lookup);