flow.c 13 KB

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