flow.c 13 KB

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