flow.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. #include <net/net_namespace.h>
  27. struct flow_cache_entry {
  28. union {
  29. struct hlist_node hlist;
  30. struct list_head gc_list;
  31. } u;
  32. struct net *net;
  33. u16 family;
  34. u8 dir;
  35. u32 genid;
  36. struct flowi key;
  37. struct flow_cache_object *object;
  38. };
  39. struct flow_flush_info {
  40. struct flow_cache *cache;
  41. atomic_t cpuleft;
  42. struct completion completion;
  43. };
  44. static struct kmem_cache *flow_cachep __read_mostly;
  45. #define flow_cache_hash_size(cache) (1 << (cache)->hash_shift)
  46. #define FLOW_HASH_RND_PERIOD (10 * 60 * HZ)
  47. static void flow_cache_new_hashrnd(unsigned long arg)
  48. {
  49. struct flow_cache *fc = (void *) arg;
  50. int i;
  51. for_each_possible_cpu(i)
  52. per_cpu_ptr(fc->percpu, i)->hash_rnd_recalc = 1;
  53. fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
  54. add_timer(&fc->rnd_timer);
  55. }
  56. static int flow_entry_valid(struct flow_cache_entry *fle,
  57. struct netns_xfrm *xfrm)
  58. {
  59. if (atomic_read(&xfrm->flow_cache_genid) != fle->genid)
  60. return 0;
  61. if (fle->object && !fle->object->ops->check(fle->object))
  62. return 0;
  63. return 1;
  64. }
  65. static void flow_entry_kill(struct flow_cache_entry *fle,
  66. struct netns_xfrm *xfrm)
  67. {
  68. if (fle->object)
  69. fle->object->ops->delete(fle->object);
  70. kmem_cache_free(flow_cachep, fle);
  71. }
  72. static void flow_cache_gc_task(struct work_struct *work)
  73. {
  74. struct list_head gc_list;
  75. struct flow_cache_entry *fce, *n;
  76. struct netns_xfrm *xfrm = container_of(work, struct netns_xfrm,
  77. flow_cache_gc_work);
  78. INIT_LIST_HEAD(&gc_list);
  79. spin_lock_bh(&xfrm->flow_cache_gc_lock);
  80. list_splice_tail_init(&xfrm->flow_cache_gc_list, &gc_list);
  81. spin_unlock_bh(&xfrm->flow_cache_gc_lock);
  82. list_for_each_entry_safe(fce, n, &gc_list, u.gc_list)
  83. flow_entry_kill(fce, xfrm);
  84. }
  85. static void flow_cache_queue_garbage(struct flow_cache_percpu *fcp,
  86. int deleted, struct list_head *gc_list,
  87. struct netns_xfrm *xfrm)
  88. {
  89. if (deleted) {
  90. fcp->hash_count -= deleted;
  91. spin_lock_bh(&xfrm->flow_cache_gc_lock);
  92. list_splice_tail(gc_list, &xfrm->flow_cache_gc_list);
  93. spin_unlock_bh(&xfrm->flow_cache_gc_lock);
  94. schedule_work(&xfrm->flow_cache_gc_work);
  95. }
  96. }
  97. static void __flow_cache_shrink(struct flow_cache *fc,
  98. struct flow_cache_percpu *fcp,
  99. int shrink_to)
  100. {
  101. struct flow_cache_entry *fle;
  102. struct hlist_node *tmp;
  103. LIST_HEAD(gc_list);
  104. int i, deleted = 0;
  105. struct netns_xfrm *xfrm = container_of(fc, struct netns_xfrm,
  106. flow_cache_global);
  107. for (i = 0; i < flow_cache_hash_size(fc); i++) {
  108. int saved = 0;
  109. hlist_for_each_entry_safe(fle, tmp,
  110. &fcp->hash_table[i], u.hlist) {
  111. if (saved < shrink_to &&
  112. flow_entry_valid(fle, xfrm)) {
  113. saved++;
  114. } else {
  115. deleted++;
  116. hlist_del(&fle->u.hlist);
  117. list_add_tail(&fle->u.gc_list, &gc_list);
  118. }
  119. }
  120. }
  121. flow_cache_queue_garbage(fcp, deleted, &gc_list, xfrm);
  122. }
  123. static void flow_cache_shrink(struct flow_cache *fc,
  124. struct flow_cache_percpu *fcp)
  125. {
  126. int shrink_to = fc->low_watermark / flow_cache_hash_size(fc);
  127. __flow_cache_shrink(fc, fcp, shrink_to);
  128. }
  129. static void flow_new_hash_rnd(struct flow_cache *fc,
  130. struct flow_cache_percpu *fcp)
  131. {
  132. get_random_bytes(&fcp->hash_rnd, sizeof(u32));
  133. fcp->hash_rnd_recalc = 0;
  134. __flow_cache_shrink(fc, fcp, 0);
  135. }
  136. static u32 flow_hash_code(struct flow_cache *fc,
  137. struct flow_cache_percpu *fcp,
  138. const struct flowi *key,
  139. size_t keysize)
  140. {
  141. const u32 *k = (const u32 *) key;
  142. const u32 length = keysize * sizeof(flow_compare_t) / sizeof(u32);
  143. return jhash2(k, length, fcp->hash_rnd)
  144. & (flow_cache_hash_size(fc) - 1);
  145. }
  146. /* I hear what you're saying, use memcmp. But memcmp cannot make
  147. * important assumptions that we can here, such as alignment.
  148. */
  149. static int flow_key_compare(const struct flowi *key1, const struct flowi *key2,
  150. size_t keysize)
  151. {
  152. const flow_compare_t *k1, *k1_lim, *k2;
  153. k1 = (const flow_compare_t *) key1;
  154. k1_lim = k1 + keysize;
  155. k2 = (const flow_compare_t *) key2;
  156. do {
  157. if (*k1++ != *k2++)
  158. return 1;
  159. } while (k1 < k1_lim);
  160. return 0;
  161. }
  162. struct flow_cache_object *
  163. flow_cache_lookup(struct net *net, const struct flowi *key, u16 family, u8 dir,
  164. flow_resolve_t resolver, void *ctx)
  165. {
  166. struct flow_cache *fc = &net->xfrm.flow_cache_global;
  167. struct flow_cache_percpu *fcp;
  168. struct flow_cache_entry *fle, *tfle;
  169. struct flow_cache_object *flo;
  170. size_t keysize;
  171. unsigned int hash;
  172. local_bh_disable();
  173. fcp = this_cpu_ptr(fc->percpu);
  174. fle = NULL;
  175. flo = NULL;
  176. keysize = flow_key_size(family);
  177. if (!keysize)
  178. goto nocache;
  179. /* Packet really early in init? Making flow_cache_init a
  180. * pre-smp initcall would solve this. --RR */
  181. if (!fcp->hash_table)
  182. goto nocache;
  183. if (fcp->hash_rnd_recalc)
  184. flow_new_hash_rnd(fc, fcp);
  185. hash = flow_hash_code(fc, fcp, key, keysize);
  186. hlist_for_each_entry(tfle, &fcp->hash_table[hash], u.hlist) {
  187. if (tfle->net == net &&
  188. tfle->family == family &&
  189. tfle->dir == dir &&
  190. flow_key_compare(key, &tfle->key, keysize) == 0) {
  191. fle = tfle;
  192. break;
  193. }
  194. }
  195. if (unlikely(!fle)) {
  196. if (fcp->hash_count > fc->high_watermark)
  197. flow_cache_shrink(fc, fcp);
  198. fle = kmem_cache_alloc(flow_cachep, GFP_ATOMIC);
  199. if (fle) {
  200. fle->net = net;
  201. fle->family = family;
  202. fle->dir = dir;
  203. memcpy(&fle->key, key, keysize * sizeof(flow_compare_t));
  204. fle->object = NULL;
  205. hlist_add_head(&fle->u.hlist, &fcp->hash_table[hash]);
  206. fcp->hash_count++;
  207. }
  208. } else if (likely(fle->genid == atomic_read(&net->xfrm.flow_cache_genid))) {
  209. flo = fle->object;
  210. if (!flo)
  211. goto ret_object;
  212. flo = flo->ops->get(flo);
  213. if (flo)
  214. goto ret_object;
  215. } else if (fle->object) {
  216. flo = fle->object;
  217. flo->ops->delete(flo);
  218. fle->object = NULL;
  219. }
  220. nocache:
  221. flo = NULL;
  222. if (fle) {
  223. flo = fle->object;
  224. fle->object = NULL;
  225. }
  226. flo = resolver(net, key, family, dir, flo, ctx);
  227. if (fle) {
  228. fle->genid = atomic_read(&net->xfrm.flow_cache_genid);
  229. if (!IS_ERR(flo))
  230. fle->object = flo;
  231. else
  232. fle->genid--;
  233. } else {
  234. if (!IS_ERR_OR_NULL(flo))
  235. flo->ops->delete(flo);
  236. }
  237. ret_object:
  238. local_bh_enable();
  239. return flo;
  240. }
  241. EXPORT_SYMBOL(flow_cache_lookup);
  242. static void flow_cache_flush_tasklet(unsigned long data)
  243. {
  244. struct flow_flush_info *info = (void *)data;
  245. struct flow_cache *fc = info->cache;
  246. struct flow_cache_percpu *fcp;
  247. struct flow_cache_entry *fle;
  248. struct hlist_node *tmp;
  249. LIST_HEAD(gc_list);
  250. int i, deleted = 0;
  251. struct netns_xfrm *xfrm = container_of(fc, struct netns_xfrm,
  252. flow_cache_global);
  253. fcp = this_cpu_ptr(fc->percpu);
  254. for (i = 0; i < flow_cache_hash_size(fc); i++) {
  255. hlist_for_each_entry_safe(fle, tmp,
  256. &fcp->hash_table[i], u.hlist) {
  257. if (flow_entry_valid(fle, xfrm))
  258. continue;
  259. deleted++;
  260. hlist_del(&fle->u.hlist);
  261. list_add_tail(&fle->u.gc_list, &gc_list);
  262. }
  263. }
  264. flow_cache_queue_garbage(fcp, deleted, &gc_list, xfrm);
  265. if (atomic_dec_and_test(&info->cpuleft))
  266. complete(&info->completion);
  267. }
  268. /*
  269. * Return whether a cpu needs flushing. Conservatively, we assume
  270. * the presence of any entries means the core may require flushing,
  271. * since the flow_cache_ops.check() function may assume it's running
  272. * on the same core as the per-cpu cache component.
  273. */
  274. static int flow_cache_percpu_empty(struct flow_cache *fc, int cpu)
  275. {
  276. struct flow_cache_percpu *fcp;
  277. int i;
  278. fcp = per_cpu_ptr(fc->percpu, cpu);
  279. for (i = 0; i < flow_cache_hash_size(fc); i++)
  280. if (!hlist_empty(&fcp->hash_table[i]))
  281. return 0;
  282. return 1;
  283. }
  284. static void flow_cache_flush_per_cpu(void *data)
  285. {
  286. struct flow_flush_info *info = data;
  287. struct tasklet_struct *tasklet;
  288. tasklet = &this_cpu_ptr(info->cache->percpu)->flush_tasklet;
  289. tasklet->data = (unsigned long)info;
  290. tasklet_schedule(tasklet);
  291. }
  292. void flow_cache_flush(struct net *net)
  293. {
  294. struct flow_flush_info info;
  295. cpumask_var_t mask;
  296. int i, self;
  297. /* Track which cpus need flushing to avoid disturbing all cores. */
  298. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  299. return;
  300. cpumask_clear(mask);
  301. /* Don't want cpus going down or up during this. */
  302. get_online_cpus();
  303. mutex_lock(&net->xfrm.flow_flush_sem);
  304. info.cache = &net->xfrm.flow_cache_global;
  305. for_each_online_cpu(i)
  306. if (!flow_cache_percpu_empty(info.cache, i))
  307. cpumask_set_cpu(i, mask);
  308. atomic_set(&info.cpuleft, cpumask_weight(mask));
  309. if (atomic_read(&info.cpuleft) == 0)
  310. goto done;
  311. init_completion(&info.completion);
  312. local_bh_disable();
  313. self = cpumask_test_and_clear_cpu(smp_processor_id(), mask);
  314. on_each_cpu_mask(mask, flow_cache_flush_per_cpu, &info, 0);
  315. if (self)
  316. flow_cache_flush_tasklet((unsigned long)&info);
  317. local_bh_enable();
  318. wait_for_completion(&info.completion);
  319. done:
  320. mutex_unlock(&net->xfrm.flow_flush_sem);
  321. put_online_cpus();
  322. free_cpumask_var(mask);
  323. }
  324. static void flow_cache_flush_task(struct work_struct *work)
  325. {
  326. struct netns_xfrm *xfrm = container_of(work, struct netns_xfrm,
  327. flow_cache_flush_work);
  328. struct net *net = container_of(xfrm, struct net, xfrm);
  329. flow_cache_flush(net);
  330. }
  331. void flow_cache_flush_deferred(struct net *net)
  332. {
  333. schedule_work(&net->xfrm.flow_cache_flush_work);
  334. }
  335. static int flow_cache_cpu_prepare(struct flow_cache *fc, int cpu)
  336. {
  337. struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, cpu);
  338. size_t sz = sizeof(struct hlist_head) * flow_cache_hash_size(fc);
  339. if (!fcp->hash_table) {
  340. fcp->hash_table = kzalloc_node(sz, GFP_KERNEL, cpu_to_node(cpu));
  341. if (!fcp->hash_table) {
  342. pr_err("NET: failed to allocate flow cache sz %zu\n", sz);
  343. return -ENOMEM;
  344. }
  345. fcp->hash_rnd_recalc = 1;
  346. fcp->hash_count = 0;
  347. tasklet_init(&fcp->flush_tasklet, flow_cache_flush_tasklet, 0);
  348. }
  349. return 0;
  350. }
  351. static int flow_cache_cpu(struct notifier_block *nfb,
  352. unsigned long action,
  353. void *hcpu)
  354. {
  355. struct flow_cache *fc = container_of(nfb, struct flow_cache,
  356. hotcpu_notifier);
  357. int res, cpu = (unsigned long) hcpu;
  358. struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, cpu);
  359. switch (action) {
  360. case CPU_UP_PREPARE:
  361. case CPU_UP_PREPARE_FROZEN:
  362. res = flow_cache_cpu_prepare(fc, cpu);
  363. if (res)
  364. return notifier_from_errno(res);
  365. break;
  366. case CPU_DEAD:
  367. case CPU_DEAD_FROZEN:
  368. __flow_cache_shrink(fc, fcp, 0);
  369. break;
  370. }
  371. return NOTIFY_OK;
  372. }
  373. int flow_cache_init(struct net *net)
  374. {
  375. int i;
  376. struct flow_cache *fc = &net->xfrm.flow_cache_global;
  377. if (!flow_cachep)
  378. flow_cachep = kmem_cache_create("flow_cache",
  379. sizeof(struct flow_cache_entry),
  380. 0, SLAB_PANIC, NULL);
  381. spin_lock_init(&net->xfrm.flow_cache_gc_lock);
  382. INIT_LIST_HEAD(&net->xfrm.flow_cache_gc_list);
  383. INIT_WORK(&net->xfrm.flow_cache_gc_work, flow_cache_gc_task);
  384. INIT_WORK(&net->xfrm.flow_cache_flush_work, flow_cache_flush_task);
  385. mutex_init(&net->xfrm.flow_flush_sem);
  386. fc->hash_shift = 10;
  387. fc->low_watermark = 2 * flow_cache_hash_size(fc);
  388. fc->high_watermark = 4 * flow_cache_hash_size(fc);
  389. fc->percpu = alloc_percpu(struct flow_cache_percpu);
  390. if (!fc->percpu)
  391. return -ENOMEM;
  392. cpu_notifier_register_begin();
  393. for_each_online_cpu(i) {
  394. if (flow_cache_cpu_prepare(fc, i))
  395. goto err;
  396. }
  397. fc->hotcpu_notifier = (struct notifier_block){
  398. .notifier_call = flow_cache_cpu,
  399. };
  400. __register_hotcpu_notifier(&fc->hotcpu_notifier);
  401. cpu_notifier_register_done();
  402. setup_timer(&fc->rnd_timer, flow_cache_new_hashrnd,
  403. (unsigned long) fc);
  404. fc->rnd_timer.expires = jiffies + FLOW_HASH_RND_PERIOD;
  405. add_timer(&fc->rnd_timer);
  406. return 0;
  407. err:
  408. for_each_possible_cpu(i) {
  409. struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, i);
  410. kfree(fcp->hash_table);
  411. fcp->hash_table = NULL;
  412. }
  413. cpu_notifier_register_done();
  414. free_percpu(fc->percpu);
  415. fc->percpu = NULL;
  416. return -ENOMEM;
  417. }
  418. EXPORT_SYMBOL(flow_cache_init);
  419. void flow_cache_fini(struct net *net)
  420. {
  421. int i;
  422. struct flow_cache *fc = &net->xfrm.flow_cache_global;
  423. del_timer_sync(&fc->rnd_timer);
  424. unregister_hotcpu_notifier(&fc->hotcpu_notifier);
  425. for_each_possible_cpu(i) {
  426. struct flow_cache_percpu *fcp = per_cpu_ptr(fc->percpu, i);
  427. kfree(fcp->hash_table);
  428. fcp->hash_table = NULL;
  429. }
  430. free_percpu(fc->percpu);
  431. fc->percpu = NULL;
  432. }
  433. EXPORT_SYMBOL(flow_cache_fini);