flow.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <linux/security.h>
  26. struct flow_cache_entry {
  27. struct flow_cache_entry *next;
  28. u16 family;
  29. u8 dir;
  30. u32 genid;
  31. struct flowi key;
  32. struct flow_cache_object *object;
  33. };
  34. struct flow_cache_percpu {
  35. struct flow_cache_entry **hash_table;
  36. int hash_count;
  37. u32 hash_rnd;
  38. int hash_rnd_recalc;
  39. struct tasklet_struct flush_tasklet;
  40. };
  41. struct flow_flush_info {
  42. struct flow_cache *cache;
  43. atomic_t cpuleft;
  44. struct completion completion;
  45. };
  46. struct flow_cache {
  47. u32 hash_shift;
  48. unsigned long order;
  49. struct flow_cache_percpu *percpu;
  50. struct notifier_block hotcpu_notifier;
  51. int low_watermark;
  52. int high_watermark;
  53. struct timer_list rnd_timer;
  54. };
  55. atomic_t flow_cache_genid = ATOMIC_INIT(0);
  56. static struct flow_cache flow_cache_global;
  57. static struct kmem_cache *flow_cachep;
  58. #define flow_cache_hash_size(cache) (1 << (cache)->hash_shift)
  59. #define FLOW_HASH_RND_PERIOD (10 * 60 * HZ)
  60. static void flow_cache_new_hashrnd(unsigned long arg)
  61. {
  62. struct flow_cache *fc = (void *) arg;
  63. int i;
  64. for_each_possible_cpu(i)
  65. per_cpu_ptr(fc->percpu, i)->hash_rnd_recalc = 1;
  66. fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
  67. add_timer(&fc->rnd_timer);
  68. }
  69. static int flow_entry_valid(struct flow_cache_entry *fle)
  70. {
  71. if (atomic_read(&flow_cache_genid) != fle->genid)
  72. return 0;
  73. if (fle->object && !fle->object->ops->check(fle->object))
  74. return 0;
  75. return 1;
  76. }
  77. static void flow_entry_kill(struct flow_cache *fc,
  78. struct flow_cache_percpu *fcp,
  79. struct flow_cache_entry *fle)
  80. {
  81. if (fle->object)
  82. fle->object->ops->delete(fle->object);
  83. kmem_cache_free(flow_cachep, fle);
  84. fcp->hash_count--;
  85. }
  86. static void __flow_cache_shrink(struct flow_cache *fc,
  87. struct flow_cache_percpu *fcp,
  88. int shrink_to)
  89. {
  90. struct flow_cache_entry *fle, **flp;
  91. int i;
  92. for (i = 0; i < flow_cache_hash_size(fc); i++) {
  93. int saved = 0;
  94. flp = &fcp->hash_table[i];
  95. while ((fle = *flp) != NULL) {
  96. if (saved < shrink_to &&
  97. flow_entry_valid(fle)) {
  98. saved++;
  99. flp = &fle->next;
  100. } else {
  101. *flp = fle->next;
  102. flow_entry_kill(fc, fcp, fle);
  103. }
  104. }
  105. }
  106. }
  107. static void flow_cache_shrink(struct flow_cache *fc,
  108. struct flow_cache_percpu *fcp)
  109. {
  110. int shrink_to = fc->low_watermark / flow_cache_hash_size(fc);
  111. __flow_cache_shrink(fc, fcp, shrink_to);
  112. }
  113. static void flow_new_hash_rnd(struct flow_cache *fc,
  114. struct flow_cache_percpu *fcp)
  115. {
  116. get_random_bytes(&fcp->hash_rnd, sizeof(u32));
  117. fcp->hash_rnd_recalc = 0;
  118. __flow_cache_shrink(fc, fcp, 0);
  119. }
  120. static u32 flow_hash_code(struct flow_cache *fc,
  121. struct flow_cache_percpu *fcp,
  122. struct flowi *key)
  123. {
  124. u32 *k = (u32 *) key;
  125. return (jhash2(k, (sizeof(*key) / sizeof(u32)), fcp->hash_rnd)
  126. & (flow_cache_hash_size(fc) - 1));
  127. }
  128. #if (BITS_PER_LONG == 64)
  129. typedef u64 flow_compare_t;
  130. #else
  131. typedef u32 flow_compare_t;
  132. #endif
  133. /* I hear what you're saying, use memcmp. But memcmp cannot make
  134. * important assumptions that we can here, such as alignment and
  135. * constant size.
  136. */
  137. static int flow_key_compare(struct flowi *key1, struct flowi *key2)
  138. {
  139. flow_compare_t *k1, *k1_lim, *k2;
  140. const int n_elem = sizeof(struct flowi) / sizeof(flow_compare_t);
  141. BUILD_BUG_ON(sizeof(struct flowi) % sizeof(flow_compare_t));
  142. k1 = (flow_compare_t *) key1;
  143. k1_lim = k1 + n_elem;
  144. k2 = (flow_compare_t *) key2;
  145. do {
  146. if (*k1++ != *k2++)
  147. return 1;
  148. } while (k1 < k1_lim);
  149. return 0;
  150. }
  151. struct flow_cache_object *
  152. flow_cache_lookup(struct net *net, struct flowi *key, u16 family, u8 dir,
  153. flow_resolve_t resolver, void *ctx)
  154. {
  155. struct flow_cache *fc = &flow_cache_global;
  156. struct flow_cache_percpu *fcp;
  157. struct flow_cache_entry *fle, **head;
  158. struct flow_cache_object *flo;
  159. unsigned int hash;
  160. local_bh_disable();
  161. fcp = per_cpu_ptr(fc->percpu, smp_processor_id());
  162. fle = NULL;
  163. flo = NULL;
  164. /* Packet really early in init? Making flow_cache_init a
  165. * pre-smp initcall would solve this. --RR */
  166. if (!fcp->hash_table)
  167. goto nocache;
  168. if (fcp->hash_rnd_recalc)
  169. flow_new_hash_rnd(fc, fcp);
  170. hash = flow_hash_code(fc, fcp, key);
  171. head = &fcp->hash_table[hash];
  172. for (fle = *head; fle; fle = fle->next) {
  173. if (fle->family == family &&
  174. fle->dir == dir &&
  175. flow_key_compare(key, &fle->key) == 0)
  176. break;
  177. }
  178. if (unlikely(!fle)) {
  179. if (fcp->hash_count > fc->high_watermark)
  180. flow_cache_shrink(fc, fcp);
  181. fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC);
  182. if (fle) {
  183. fle->next = *head;
  184. *head = fle;
  185. fle->family = family;
  186. fle->dir = dir;
  187. memcpy(&fle->key, key, sizeof(*key));
  188. fle->object = NULL;
  189. fcp->hash_count++;
  190. }
  191. } else if (likely(fle->genid == atomic_read(&flow_cache_genid))) {
  192. flo = fle->object;
  193. if (!flo)
  194. goto ret_object;
  195. flo = flo->ops->get(flo);
  196. if (flo)
  197. goto ret_object;
  198. } else if (fle->object) {
  199. flo = fle->object;
  200. flo->ops->delete(flo);
  201. fle->object = NULL;
  202. }
  203. nocache:
  204. flo = NULL;
  205. if (fle) {
  206. flo = fle->object;
  207. fle->object = NULL;
  208. }
  209. flo = resolver(net, key, family, dir, flo, ctx);
  210. if (fle) {
  211. fle->genid = atomic_read(&flow_cache_genid);
  212. if (!IS_ERR(flo))
  213. fle->object = flo;
  214. else
  215. fle->genid--;
  216. } else {
  217. if (flo && !IS_ERR(flo))
  218. flo->ops->delete(flo);
  219. }
  220. ret_object:
  221. local_bh_enable();
  222. return flo;
  223. }
  224. static void flow_cache_flush_tasklet(unsigned long data)
  225. {
  226. struct flow_flush_info *info = (void *)data;
  227. struct flow_cache *fc = info->cache;
  228. struct flow_cache_percpu *fcp;
  229. int i;
  230. fcp = per_cpu_ptr(fc->percpu, smp_processor_id());
  231. for (i = 0; i < flow_cache_hash_size(fc); i++) {
  232. struct flow_cache_entry *fle;
  233. fle = fcp->hash_table[i];
  234. for (; fle; fle = fle->next) {
  235. if (flow_entry_valid(fle))
  236. continue;
  237. if (fle->object)
  238. fle->object->ops->delete(fle->object);
  239. fle->object = NULL;
  240. }
  241. }
  242. if (atomic_dec_and_test(&info->cpuleft))
  243. complete(&info->completion);
  244. }
  245. static void flow_cache_flush_per_cpu(void *data)
  246. {
  247. struct flow_flush_info *info = data;
  248. int cpu;
  249. struct tasklet_struct *tasklet;
  250. cpu = smp_processor_id();
  251. tasklet = &per_cpu_ptr(info->cache->percpu, cpu)->flush_tasklet;
  252. tasklet->data = (unsigned long)info;
  253. tasklet_schedule(tasklet);
  254. }
  255. void flow_cache_flush(void)
  256. {
  257. struct flow_flush_info info;
  258. static DEFINE_MUTEX(flow_flush_sem);
  259. /* Don't want cpus going down or up during this. */
  260. get_online_cpus();
  261. mutex_lock(&flow_flush_sem);
  262. info.cache = &flow_cache_global;
  263. atomic_set(&info.cpuleft, num_online_cpus());
  264. init_completion(&info.completion);
  265. local_bh_disable();
  266. smp_call_function(flow_cache_flush_per_cpu, &info, 0);
  267. flow_cache_flush_tasklet((unsigned long)&info);
  268. local_bh_enable();
  269. wait_for_completion(&info.completion);
  270. mutex_unlock(&flow_flush_sem);
  271. put_online_cpus();
  272. }
  273. static void __init flow_cache_cpu_prepare(struct flow_cache *fc,
  274. struct flow_cache_percpu *fcp)
  275. {
  276. fcp->hash_table = (struct flow_cache_entry **)
  277. __get_free_pages(GFP_KERNEL|__GFP_ZERO, fc->order);
  278. if (!fcp->hash_table)
  279. panic("NET: failed to allocate flow cache order %lu\n", fc->order);
  280. fcp->hash_rnd_recalc = 1;
  281. fcp->hash_count = 0;
  282. tasklet_init(&fcp->flush_tasklet, flow_cache_flush_tasklet, 0);
  283. }
  284. static int flow_cache_cpu(struct notifier_block *nfb,
  285. unsigned long action,
  286. void *hcpu)
  287. {
  288. struct flow_cache *fc = container_of(nfb, struct flow_cache, hotcpu_notifier);
  289. int cpu = (unsigned long) hcpu;
  290. struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, cpu);
  291. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
  292. __flow_cache_shrink(fc, fcp, 0);
  293. return NOTIFY_OK;
  294. }
  295. static int flow_cache_init(struct flow_cache *fc)
  296. {
  297. unsigned long order;
  298. int i;
  299. fc->hash_shift = 10;
  300. fc->low_watermark = 2 * flow_cache_hash_size(fc);
  301. fc->high_watermark = 4 * flow_cache_hash_size(fc);
  302. for (order = 0;
  303. (PAGE_SIZE << order) <
  304. (sizeof(struct flow_cache_entry *)*flow_cache_hash_size(fc));
  305. order++)
  306. /* NOTHING */;
  307. fc->order = order;
  308. fc->percpu = alloc_percpu(struct flow_cache_percpu);
  309. setup_timer(&fc->rnd_timer, flow_cache_new_hashrnd,
  310. (unsigned long) fc);
  311. fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
  312. add_timer(&fc->rnd_timer);
  313. for_each_possible_cpu(i)
  314. flow_cache_cpu_prepare(fc, per_cpu_ptr(fc->percpu, i));
  315. fc->hotcpu_notifier = (struct notifier_block){
  316. .notifier_call = flow_cache_cpu,
  317. };
  318. register_hotcpu_notifier(&fc->hotcpu_notifier);
  319. return 0;
  320. }
  321. static int __init flow_cache_init_global(void)
  322. {
  323. flow_cachep = kmem_cache_create("flow_cache",
  324. sizeof(struct flow_cache_entry),
  325. 0, SLAB_PANIC, NULL);
  326. return flow_cache_init(&flow_cache_global);
  327. }
  328. module_init(flow_cache_init_global);
  329. EXPORT_SYMBOL(flow_cache_genid);
  330. EXPORT_SYMBOL(flow_cache_lookup);