flow.c 13 KB

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