flow.c 10 KB

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