flow_table.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2007-2014 Nicira, Inc.
  4. */
  5. #include "flow.h"
  6. #include "datapath.h"
  7. #include "flow_netlink.h"
  8. #include <linux/uaccess.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/if_ether.h>
  12. #include <linux/if_vlan.h>
  13. #include <net/llc_pdu.h>
  14. #include <linux/kernel.h>
  15. #include <linux/jhash.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/llc.h>
  18. #include <linux/module.h>
  19. #include <linux/in.h>
  20. #include <linux/rcupdate.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/ip.h>
  24. #include <linux/ipv6.h>
  25. #include <linux/sctp.h>
  26. #include <linux/tcp.h>
  27. #include <linux/udp.h>
  28. #include <linux/icmp.h>
  29. #include <linux/icmpv6.h>
  30. #include <linux/rculist.h>
  31. #include <linux/sort.h>
  32. #include <net/ip.h>
  33. #include <net/ipv6.h>
  34. #include <net/ndisc.h>
  35. #define TBL_MIN_BUCKETS 1024
  36. #define MASK_ARRAY_SIZE_MIN 16
  37. #define REHASH_INTERVAL (10 * 60 * HZ)
  38. #define MC_DEFAULT_HASH_ENTRIES 256
  39. #define MC_HASH_SHIFT 8
  40. #define MC_HASH_SEGS ((sizeof(uint32_t) * 8) / MC_HASH_SHIFT)
  41. static struct kmem_cache *flow_cache;
  42. struct kmem_cache *flow_stats_cache __read_mostly;
  43. static u16 range_n_bytes(const struct sw_flow_key_range *range)
  44. {
  45. return range->end - range->start;
  46. }
  47. void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
  48. bool full, const struct sw_flow_mask *mask)
  49. {
  50. int start = full ? 0 : mask->range.start;
  51. int len = full ? sizeof *dst : range_n_bytes(&mask->range);
  52. const long *m = (const long *)((const u8 *)&mask->key + start);
  53. const long *s = (const long *)((const u8 *)src + start);
  54. long *d = (long *)((u8 *)dst + start);
  55. int i;
  56. /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
  57. * if 'full' is false the memory outside of the 'mask->range' is left
  58. * uninitialized. This can be used as an optimization when further
  59. * operations on 'dst' only use contents within 'mask->range'.
  60. */
  61. for (i = 0; i < len; i += sizeof(long))
  62. *d++ = *s++ & *m++;
  63. }
  64. struct sw_flow *ovs_flow_alloc(void)
  65. {
  66. struct sw_flow *flow;
  67. struct sw_flow_stats *stats;
  68. flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL);
  69. if (!flow)
  70. return ERR_PTR(-ENOMEM);
  71. flow->stats_last_writer = -1;
  72. /* Initialize the default stat node. */
  73. stats = kmem_cache_alloc_node(flow_stats_cache,
  74. GFP_KERNEL | __GFP_ZERO,
  75. node_online(0) ? 0 : NUMA_NO_NODE);
  76. if (!stats)
  77. goto err;
  78. spin_lock_init(&stats->lock);
  79. RCU_INIT_POINTER(flow->stats[0], stats);
  80. cpumask_set_cpu(0, &flow->cpu_used_mask);
  81. return flow;
  82. err:
  83. kmem_cache_free(flow_cache, flow);
  84. return ERR_PTR(-ENOMEM);
  85. }
  86. int ovs_flow_tbl_count(const struct flow_table *table)
  87. {
  88. return table->count;
  89. }
  90. static void flow_free(struct sw_flow *flow)
  91. {
  92. int cpu;
  93. if (ovs_identifier_is_key(&flow->id))
  94. kfree(flow->id.unmasked_key);
  95. if (flow->sf_acts)
  96. ovs_nla_free_flow_actions((struct sw_flow_actions __force *)
  97. flow->sf_acts);
  98. /* We open code this to make sure cpu 0 is always considered */
  99. for (cpu = 0; cpu < nr_cpu_ids;
  100. cpu = cpumask_next(cpu, &flow->cpu_used_mask)) {
  101. if (flow->stats[cpu])
  102. kmem_cache_free(flow_stats_cache,
  103. (struct sw_flow_stats __force *)flow->stats[cpu]);
  104. }
  105. kmem_cache_free(flow_cache, flow);
  106. }
  107. static void rcu_free_flow_callback(struct rcu_head *rcu)
  108. {
  109. struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
  110. flow_free(flow);
  111. }
  112. void ovs_flow_free(struct sw_flow *flow, bool deferred)
  113. {
  114. if (!flow)
  115. return;
  116. if (deferred)
  117. call_rcu(&flow->rcu, rcu_free_flow_callback);
  118. else
  119. flow_free(flow);
  120. }
  121. static void __table_instance_destroy(struct table_instance *ti)
  122. {
  123. kvfree(ti->buckets);
  124. kfree(ti);
  125. }
  126. static struct table_instance *table_instance_alloc(int new_size)
  127. {
  128. struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
  129. int i;
  130. if (!ti)
  131. return NULL;
  132. ti->buckets = kvmalloc_array(new_size, sizeof(struct hlist_head),
  133. GFP_KERNEL);
  134. if (!ti->buckets) {
  135. kfree(ti);
  136. return NULL;
  137. }
  138. for (i = 0; i < new_size; i++)
  139. INIT_HLIST_HEAD(&ti->buckets[i]);
  140. ti->n_buckets = new_size;
  141. ti->node_ver = 0;
  142. get_random_bytes(&ti->hash_seed, sizeof(u32));
  143. return ti;
  144. }
  145. static void __mask_array_destroy(struct mask_array *ma)
  146. {
  147. free_percpu(ma->masks_usage_stats);
  148. kfree(ma);
  149. }
  150. static void mask_array_rcu_cb(struct rcu_head *rcu)
  151. {
  152. struct mask_array *ma = container_of(rcu, struct mask_array, rcu);
  153. __mask_array_destroy(ma);
  154. }
  155. static void tbl_mask_array_reset_counters(struct mask_array *ma)
  156. {
  157. int i, cpu;
  158. /* As the per CPU counters are not atomic we can not go ahead and
  159. * reset them from another CPU. To be able to still have an approximate
  160. * zero based counter we store the value at reset, and subtract it
  161. * later when processing.
  162. */
  163. for (i = 0; i < ma->max; i++) {
  164. ma->masks_usage_zero_cntr[i] = 0;
  165. for_each_possible_cpu(cpu) {
  166. struct mask_array_stats *stats;
  167. unsigned int start;
  168. u64 counter;
  169. stats = per_cpu_ptr(ma->masks_usage_stats, cpu);
  170. do {
  171. start = u64_stats_fetch_begin_irq(&stats->syncp);
  172. counter = stats->usage_cntrs[i];
  173. } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
  174. ma->masks_usage_zero_cntr[i] += counter;
  175. }
  176. }
  177. }
  178. static struct mask_array *tbl_mask_array_alloc(int size)
  179. {
  180. struct mask_array *new;
  181. size = max(MASK_ARRAY_SIZE_MIN, size);
  182. new = kzalloc(sizeof(struct mask_array) +
  183. sizeof(struct sw_flow_mask *) * size +
  184. sizeof(u64) * size, GFP_KERNEL);
  185. if (!new)
  186. return NULL;
  187. new->masks_usage_zero_cntr = (u64 *)((u8 *)new +
  188. sizeof(struct mask_array) +
  189. sizeof(struct sw_flow_mask *) *
  190. size);
  191. new->masks_usage_stats = __alloc_percpu(sizeof(struct mask_array_stats) +
  192. sizeof(u64) * size,
  193. __alignof__(u64));
  194. if (!new->masks_usage_stats) {
  195. kfree(new);
  196. return NULL;
  197. }
  198. new->count = 0;
  199. new->max = size;
  200. return new;
  201. }
  202. static int tbl_mask_array_realloc(struct flow_table *tbl, int size)
  203. {
  204. struct mask_array *old;
  205. struct mask_array *new;
  206. new = tbl_mask_array_alloc(size);
  207. if (!new)
  208. return -ENOMEM;
  209. old = ovsl_dereference(tbl->mask_array);
  210. if (old) {
  211. int i;
  212. for (i = 0; i < old->max; i++) {
  213. if (ovsl_dereference(old->masks[i]))
  214. new->masks[new->count++] = old->masks[i];
  215. }
  216. call_rcu(&old->rcu, mask_array_rcu_cb);
  217. }
  218. rcu_assign_pointer(tbl->mask_array, new);
  219. return 0;
  220. }
  221. static int tbl_mask_array_add_mask(struct flow_table *tbl,
  222. struct sw_flow_mask *new)
  223. {
  224. struct mask_array *ma = ovsl_dereference(tbl->mask_array);
  225. int err, ma_count = READ_ONCE(ma->count);
  226. if (ma_count >= ma->max) {
  227. err = tbl_mask_array_realloc(tbl, ma->max +
  228. MASK_ARRAY_SIZE_MIN);
  229. if (err)
  230. return err;
  231. ma = ovsl_dereference(tbl->mask_array);
  232. } else {
  233. /* On every add or delete we need to reset the counters so
  234. * every new mask gets a fair chance of being prioritized.
  235. */
  236. tbl_mask_array_reset_counters(ma);
  237. }
  238. BUG_ON(ovsl_dereference(ma->masks[ma_count]));
  239. rcu_assign_pointer(ma->masks[ma_count], new);
  240. WRITE_ONCE(ma->count, ma_count + 1);
  241. return 0;
  242. }
  243. static void tbl_mask_array_del_mask(struct flow_table *tbl,
  244. struct sw_flow_mask *mask)
  245. {
  246. struct mask_array *ma = ovsl_dereference(tbl->mask_array);
  247. int i, ma_count = READ_ONCE(ma->count);
  248. /* Remove the deleted mask pointers from the array */
  249. for (i = 0; i < ma_count; i++) {
  250. if (mask == ovsl_dereference(ma->masks[i]))
  251. goto found;
  252. }
  253. BUG();
  254. return;
  255. found:
  256. WRITE_ONCE(ma->count, ma_count - 1);
  257. rcu_assign_pointer(ma->masks[i], ma->masks[ma_count - 1]);
  258. RCU_INIT_POINTER(ma->masks[ma_count - 1], NULL);
  259. kfree_rcu(mask, rcu);
  260. /* Shrink the mask array if necessary. */
  261. if (ma->max >= (MASK_ARRAY_SIZE_MIN * 2) &&
  262. ma_count <= (ma->max / 3))
  263. tbl_mask_array_realloc(tbl, ma->max / 2);
  264. else
  265. tbl_mask_array_reset_counters(ma);
  266. }
  267. /* Remove 'mask' from the mask list, if it is not needed any more. */
  268. static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
  269. {
  270. if (mask) {
  271. /* ovs-lock is required to protect mask-refcount and
  272. * mask list.
  273. */
  274. ASSERT_OVSL();
  275. BUG_ON(!mask->ref_count);
  276. mask->ref_count--;
  277. if (!mask->ref_count)
  278. tbl_mask_array_del_mask(tbl, mask);
  279. }
  280. }
  281. static void __mask_cache_destroy(struct mask_cache *mc)
  282. {
  283. free_percpu(mc->mask_cache);
  284. kfree(mc);
  285. }
  286. static void mask_cache_rcu_cb(struct rcu_head *rcu)
  287. {
  288. struct mask_cache *mc = container_of(rcu, struct mask_cache, rcu);
  289. __mask_cache_destroy(mc);
  290. }
  291. static struct mask_cache *tbl_mask_cache_alloc(u32 size)
  292. {
  293. struct mask_cache_entry __percpu *cache = NULL;
  294. struct mask_cache *new;
  295. /* Only allow size to be 0, or a power of 2, and does not exceed
  296. * percpu allocation size.
  297. */
  298. if ((!is_power_of_2(size) && size != 0) ||
  299. (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)
  300. return NULL;
  301. new = kzalloc(sizeof(*new), GFP_KERNEL);
  302. if (!new)
  303. return NULL;
  304. new->cache_size = size;
  305. if (new->cache_size > 0) {
  306. cache = __alloc_percpu(array_size(sizeof(struct mask_cache_entry),
  307. new->cache_size),
  308. __alignof__(struct mask_cache_entry));
  309. if (!cache) {
  310. kfree(new);
  311. return NULL;
  312. }
  313. }
  314. new->mask_cache = cache;
  315. return new;
  316. }
  317. int ovs_flow_tbl_masks_cache_resize(struct flow_table *table, u32 size)
  318. {
  319. struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache);
  320. struct mask_cache *new;
  321. if (size == mc->cache_size)
  322. return 0;
  323. if ((!is_power_of_2(size) && size != 0) ||
  324. (size * sizeof(struct mask_cache_entry)) > PCPU_MIN_UNIT_SIZE)
  325. return -EINVAL;
  326. new = tbl_mask_cache_alloc(size);
  327. if (!new)
  328. return -ENOMEM;
  329. rcu_assign_pointer(table->mask_cache, new);
  330. call_rcu(&mc->rcu, mask_cache_rcu_cb);
  331. return 0;
  332. }
  333. int ovs_flow_tbl_init(struct flow_table *table)
  334. {
  335. struct table_instance *ti, *ufid_ti;
  336. struct mask_cache *mc;
  337. struct mask_array *ma;
  338. mc = tbl_mask_cache_alloc(MC_DEFAULT_HASH_ENTRIES);
  339. if (!mc)
  340. return -ENOMEM;
  341. ma = tbl_mask_array_alloc(MASK_ARRAY_SIZE_MIN);
  342. if (!ma)
  343. goto free_mask_cache;
  344. ti = table_instance_alloc(TBL_MIN_BUCKETS);
  345. if (!ti)
  346. goto free_mask_array;
  347. ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
  348. if (!ufid_ti)
  349. goto free_ti;
  350. rcu_assign_pointer(table->ti, ti);
  351. rcu_assign_pointer(table->ufid_ti, ufid_ti);
  352. rcu_assign_pointer(table->mask_array, ma);
  353. rcu_assign_pointer(table->mask_cache, mc);
  354. table->last_rehash = jiffies;
  355. table->count = 0;
  356. table->ufid_count = 0;
  357. return 0;
  358. free_ti:
  359. __table_instance_destroy(ti);
  360. free_mask_array:
  361. __mask_array_destroy(ma);
  362. free_mask_cache:
  363. __mask_cache_destroy(mc);
  364. return -ENOMEM;
  365. }
  366. static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
  367. {
  368. struct table_instance *ti;
  369. ti = container_of(rcu, struct table_instance, rcu);
  370. __table_instance_destroy(ti);
  371. }
  372. static void table_instance_flow_free(struct flow_table *table,
  373. struct table_instance *ti,
  374. struct table_instance *ufid_ti,
  375. struct sw_flow *flow)
  376. {
  377. hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
  378. table->count--;
  379. if (ovs_identifier_is_ufid(&flow->id)) {
  380. hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
  381. table->ufid_count--;
  382. }
  383. flow_mask_remove(table, flow->mask);
  384. }
  385. /* Must be called with OVS mutex held. */
  386. void table_instance_flow_flush(struct flow_table *table,
  387. struct table_instance *ti,
  388. struct table_instance *ufid_ti)
  389. {
  390. int i;
  391. for (i = 0; i < ti->n_buckets; i++) {
  392. struct hlist_head *head = &ti->buckets[i];
  393. struct hlist_node *n;
  394. struct sw_flow *flow;
  395. hlist_for_each_entry_safe(flow, n, head,
  396. flow_table.node[ti->node_ver]) {
  397. table_instance_flow_free(table, ti, ufid_ti,
  398. flow);
  399. ovs_flow_free(flow, true);
  400. }
  401. }
  402. if (WARN_ON(table->count != 0 ||
  403. table->ufid_count != 0)) {
  404. table->count = 0;
  405. table->ufid_count = 0;
  406. }
  407. }
  408. static void table_instance_destroy(struct table_instance *ti,
  409. struct table_instance *ufid_ti)
  410. {
  411. call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
  412. call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
  413. }
  414. /* No need for locking this function is called from RCU callback or
  415. * error path.
  416. */
  417. void ovs_flow_tbl_destroy(struct flow_table *table)
  418. {
  419. struct table_instance *ti = rcu_dereference_raw(table->ti);
  420. struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
  421. struct mask_cache *mc = rcu_dereference_raw(table->mask_cache);
  422. struct mask_array *ma = rcu_dereference_raw(table->mask_array);
  423. call_rcu(&mc->rcu, mask_cache_rcu_cb);
  424. call_rcu(&ma->rcu, mask_array_rcu_cb);
  425. table_instance_destroy(ti, ufid_ti);
  426. }
  427. struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
  428. u32 *bucket, u32 *last)
  429. {
  430. struct sw_flow *flow;
  431. struct hlist_head *head;
  432. int ver;
  433. int i;
  434. ver = ti->node_ver;
  435. while (*bucket < ti->n_buckets) {
  436. i = 0;
  437. head = &ti->buckets[*bucket];
  438. hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
  439. if (i < *last) {
  440. i++;
  441. continue;
  442. }
  443. *last = i + 1;
  444. return flow;
  445. }
  446. (*bucket)++;
  447. *last = 0;
  448. }
  449. return NULL;
  450. }
  451. static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
  452. {
  453. hash = jhash_1word(hash, ti->hash_seed);
  454. return &ti->buckets[hash & (ti->n_buckets - 1)];
  455. }
  456. static void table_instance_insert(struct table_instance *ti,
  457. struct sw_flow *flow)
  458. {
  459. struct hlist_head *head;
  460. head = find_bucket(ti, flow->flow_table.hash);
  461. hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
  462. }
  463. static void ufid_table_instance_insert(struct table_instance *ti,
  464. struct sw_flow *flow)
  465. {
  466. struct hlist_head *head;
  467. head = find_bucket(ti, flow->ufid_table.hash);
  468. hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
  469. }
  470. static void flow_table_copy_flows(struct table_instance *old,
  471. struct table_instance *new, bool ufid)
  472. {
  473. int old_ver;
  474. int i;
  475. old_ver = old->node_ver;
  476. new->node_ver = !old_ver;
  477. /* Insert in new table. */
  478. for (i = 0; i < old->n_buckets; i++) {
  479. struct sw_flow *flow;
  480. struct hlist_head *head = &old->buckets[i];
  481. if (ufid)
  482. hlist_for_each_entry_rcu(flow, head,
  483. ufid_table.node[old_ver],
  484. lockdep_ovsl_is_held())
  485. ufid_table_instance_insert(new, flow);
  486. else
  487. hlist_for_each_entry_rcu(flow, head,
  488. flow_table.node[old_ver],
  489. lockdep_ovsl_is_held())
  490. table_instance_insert(new, flow);
  491. }
  492. }
  493. static struct table_instance *table_instance_rehash(struct table_instance *ti,
  494. int n_buckets, bool ufid)
  495. {
  496. struct table_instance *new_ti;
  497. new_ti = table_instance_alloc(n_buckets);
  498. if (!new_ti)
  499. return NULL;
  500. flow_table_copy_flows(ti, new_ti, ufid);
  501. return new_ti;
  502. }
  503. int ovs_flow_tbl_flush(struct flow_table *flow_table)
  504. {
  505. struct table_instance *old_ti, *new_ti;
  506. struct table_instance *old_ufid_ti, *new_ufid_ti;
  507. new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
  508. if (!new_ti)
  509. return -ENOMEM;
  510. new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
  511. if (!new_ufid_ti)
  512. goto err_free_ti;
  513. old_ti = ovsl_dereference(flow_table->ti);
  514. old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
  515. rcu_assign_pointer(flow_table->ti, new_ti);
  516. rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
  517. flow_table->last_rehash = jiffies;
  518. table_instance_flow_flush(flow_table, old_ti, old_ufid_ti);
  519. table_instance_destroy(old_ti, old_ufid_ti);
  520. return 0;
  521. err_free_ti:
  522. __table_instance_destroy(new_ti);
  523. return -ENOMEM;
  524. }
  525. static u32 flow_hash(const struct sw_flow_key *key,
  526. const struct sw_flow_key_range *range)
  527. {
  528. const u32 *hash_key = (const u32 *)((const u8 *)key + range->start);
  529. /* Make sure number of hash bytes are multiple of u32. */
  530. int hash_u32s = range_n_bytes(range) >> 2;
  531. return jhash2(hash_key, hash_u32s, 0);
  532. }
  533. static int flow_key_start(const struct sw_flow_key *key)
  534. {
  535. if (key->tun_proto)
  536. return 0;
  537. else
  538. return rounddown(offsetof(struct sw_flow_key, phy),
  539. sizeof(long));
  540. }
  541. static bool cmp_key(const struct sw_flow_key *key1,
  542. const struct sw_flow_key *key2,
  543. int key_start, int key_end)
  544. {
  545. const long *cp1 = (const long *)((const u8 *)key1 + key_start);
  546. const long *cp2 = (const long *)((const u8 *)key2 + key_start);
  547. long diffs = 0;
  548. int i;
  549. for (i = key_start; i < key_end; i += sizeof(long))
  550. diffs |= *cp1++ ^ *cp2++;
  551. return diffs == 0;
  552. }
  553. static bool flow_cmp_masked_key(const struct sw_flow *flow,
  554. const struct sw_flow_key *key,
  555. const struct sw_flow_key_range *range)
  556. {
  557. return cmp_key(&flow->key, key, range->start, range->end);
  558. }
  559. static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
  560. const struct sw_flow_match *match)
  561. {
  562. struct sw_flow_key *key = match->key;
  563. int key_start = flow_key_start(key);
  564. int key_end = match->range.end;
  565. BUG_ON(ovs_identifier_is_ufid(&flow->id));
  566. return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
  567. }
  568. static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
  569. const struct sw_flow_key *unmasked,
  570. const struct sw_flow_mask *mask,
  571. u32 *n_mask_hit)
  572. {
  573. struct sw_flow *flow;
  574. struct hlist_head *head;
  575. u32 hash;
  576. struct sw_flow_key masked_key;
  577. ovs_flow_mask_key(&masked_key, unmasked, false, mask);
  578. hash = flow_hash(&masked_key, &mask->range);
  579. head = find_bucket(ti, hash);
  580. (*n_mask_hit)++;
  581. hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver],
  582. lockdep_ovsl_is_held()) {
  583. if (flow->mask == mask && flow->flow_table.hash == hash &&
  584. flow_cmp_masked_key(flow, &masked_key, &mask->range))
  585. return flow;
  586. }
  587. return NULL;
  588. }
  589. /* Flow lookup does full lookup on flow table. It starts with
  590. * mask from index passed in *index.
  591. * This function MUST be called with BH disabled due to the use
  592. * of CPU specific variables.
  593. */
  594. static struct sw_flow *flow_lookup(struct flow_table *tbl,
  595. struct table_instance *ti,
  596. struct mask_array *ma,
  597. const struct sw_flow_key *key,
  598. u32 *n_mask_hit,
  599. u32 *n_cache_hit,
  600. u32 *index)
  601. {
  602. struct mask_array_stats *stats = this_cpu_ptr(ma->masks_usage_stats);
  603. struct sw_flow *flow;
  604. struct sw_flow_mask *mask;
  605. int i;
  606. if (likely(*index < ma->max)) {
  607. mask = rcu_dereference_ovsl(ma->masks[*index]);
  608. if (mask) {
  609. flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
  610. if (flow) {
  611. u64_stats_update_begin(&stats->syncp);
  612. stats->usage_cntrs[*index]++;
  613. u64_stats_update_end(&stats->syncp);
  614. (*n_cache_hit)++;
  615. return flow;
  616. }
  617. }
  618. }
  619. for (i = 0; i < ma->max; i++) {
  620. if (i == *index)
  621. continue;
  622. mask = rcu_dereference_ovsl(ma->masks[i]);
  623. if (unlikely(!mask))
  624. break;
  625. flow = masked_flow_lookup(ti, key, mask, n_mask_hit);
  626. if (flow) { /* Found */
  627. *index = i;
  628. u64_stats_update_begin(&stats->syncp);
  629. stats->usage_cntrs[*index]++;
  630. u64_stats_update_end(&stats->syncp);
  631. return flow;
  632. }
  633. }
  634. return NULL;
  635. }
  636. /*
  637. * mask_cache maps flow to probable mask. This cache is not tightly
  638. * coupled cache, It means updates to mask list can result in inconsistent
  639. * cache entry in mask cache.
  640. * This is per cpu cache and is divided in MC_HASH_SEGS segments.
  641. * In case of a hash collision the entry is hashed in next segment.
  642. * */
  643. struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
  644. const struct sw_flow_key *key,
  645. u32 skb_hash,
  646. u32 *n_mask_hit,
  647. u32 *n_cache_hit)
  648. {
  649. struct mask_cache *mc = rcu_dereference(tbl->mask_cache);
  650. struct mask_array *ma = rcu_dereference(tbl->mask_array);
  651. struct table_instance *ti = rcu_dereference(tbl->ti);
  652. struct mask_cache_entry *entries, *ce;
  653. struct sw_flow *flow;
  654. u32 hash;
  655. int seg;
  656. *n_mask_hit = 0;
  657. *n_cache_hit = 0;
  658. if (unlikely(!skb_hash || mc->cache_size == 0)) {
  659. u32 mask_index = 0;
  660. u32 cache = 0;
  661. return flow_lookup(tbl, ti, ma, key, n_mask_hit, &cache,
  662. &mask_index);
  663. }
  664. /* Pre and post recirulation flows usually have the same skb_hash
  665. * value. To avoid hash collisions, rehash the 'skb_hash' with
  666. * 'recirc_id'. */
  667. if (key->recirc_id)
  668. skb_hash = jhash_1word(skb_hash, key->recirc_id);
  669. ce = NULL;
  670. hash = skb_hash;
  671. entries = this_cpu_ptr(mc->mask_cache);
  672. /* Find the cache entry 'ce' to operate on. */
  673. for (seg = 0; seg < MC_HASH_SEGS; seg++) {
  674. int index = hash & (mc->cache_size - 1);
  675. struct mask_cache_entry *e;
  676. e = &entries[index];
  677. if (e->skb_hash == skb_hash) {
  678. flow = flow_lookup(tbl, ti, ma, key, n_mask_hit,
  679. n_cache_hit, &e->mask_index);
  680. if (!flow)
  681. e->skb_hash = 0;
  682. return flow;
  683. }
  684. if (!ce || e->skb_hash < ce->skb_hash)
  685. ce = e; /* A better replacement cache candidate. */
  686. hash >>= MC_HASH_SHIFT;
  687. }
  688. /* Cache miss, do full lookup. */
  689. flow = flow_lookup(tbl, ti, ma, key, n_mask_hit, n_cache_hit,
  690. &ce->mask_index);
  691. if (flow)
  692. ce->skb_hash = skb_hash;
  693. *n_cache_hit = 0;
  694. return flow;
  695. }
  696. struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
  697. const struct sw_flow_key *key)
  698. {
  699. struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
  700. struct mask_array *ma = rcu_dereference_ovsl(tbl->mask_array);
  701. u32 __always_unused n_mask_hit;
  702. u32 __always_unused n_cache_hit;
  703. struct sw_flow *flow;
  704. u32 index = 0;
  705. /* This function gets called trough the netlink interface and therefore
  706. * is preemptible. However, flow_lookup() function needs to be called
  707. * with BH disabled due to CPU specific variables.
  708. */
  709. local_bh_disable();
  710. flow = flow_lookup(tbl, ti, ma, key, &n_mask_hit, &n_cache_hit, &index);
  711. local_bh_enable();
  712. return flow;
  713. }
  714. struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
  715. const struct sw_flow_match *match)
  716. {
  717. struct mask_array *ma = ovsl_dereference(tbl->mask_array);
  718. int i;
  719. /* Always called under ovs-mutex. */
  720. for (i = 0; i < ma->max; i++) {
  721. struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
  722. u32 __always_unused n_mask_hit;
  723. struct sw_flow_mask *mask;
  724. struct sw_flow *flow;
  725. mask = ovsl_dereference(ma->masks[i]);
  726. if (!mask)
  727. continue;
  728. flow = masked_flow_lookup(ti, match->key, mask, &n_mask_hit);
  729. if (flow && ovs_identifier_is_key(&flow->id) &&
  730. ovs_flow_cmp_unmasked_key(flow, match)) {
  731. return flow;
  732. }
  733. }
  734. return NULL;
  735. }
  736. static u32 ufid_hash(const struct sw_flow_id *sfid)
  737. {
  738. return jhash(sfid->ufid, sfid->ufid_len, 0);
  739. }
  740. static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
  741. const struct sw_flow_id *sfid)
  742. {
  743. if (flow->id.ufid_len != sfid->ufid_len)
  744. return false;
  745. return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
  746. }
  747. bool ovs_flow_cmp(const struct sw_flow *flow,
  748. const struct sw_flow_match *match)
  749. {
  750. if (ovs_identifier_is_ufid(&flow->id))
  751. return flow_cmp_masked_key(flow, match->key, &match->range);
  752. return ovs_flow_cmp_unmasked_key(flow, match);
  753. }
  754. struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
  755. const struct sw_flow_id *ufid)
  756. {
  757. struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
  758. struct sw_flow *flow;
  759. struct hlist_head *head;
  760. u32 hash;
  761. hash = ufid_hash(ufid);
  762. head = find_bucket(ti, hash);
  763. hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver],
  764. lockdep_ovsl_is_held()) {
  765. if (flow->ufid_table.hash == hash &&
  766. ovs_flow_cmp_ufid(flow, ufid))
  767. return flow;
  768. }
  769. return NULL;
  770. }
  771. int ovs_flow_tbl_num_masks(const struct flow_table *table)
  772. {
  773. struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
  774. return READ_ONCE(ma->count);
  775. }
  776. u32 ovs_flow_tbl_masks_cache_size(const struct flow_table *table)
  777. {
  778. struct mask_cache *mc = rcu_dereference_ovsl(table->mask_cache);
  779. return READ_ONCE(mc->cache_size);
  780. }
  781. static struct table_instance *table_instance_expand(struct table_instance *ti,
  782. bool ufid)
  783. {
  784. return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
  785. }
  786. /* Must be called with OVS mutex held. */
  787. void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
  788. {
  789. struct table_instance *ti = ovsl_dereference(table->ti);
  790. struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
  791. BUG_ON(table->count == 0);
  792. table_instance_flow_free(table, ti, ufid_ti, flow);
  793. }
  794. static struct sw_flow_mask *mask_alloc(void)
  795. {
  796. struct sw_flow_mask *mask;
  797. mask = kmalloc(sizeof(*mask), GFP_KERNEL);
  798. if (mask)
  799. mask->ref_count = 1;
  800. return mask;
  801. }
  802. static bool mask_equal(const struct sw_flow_mask *a,
  803. const struct sw_flow_mask *b)
  804. {
  805. const u8 *a_ = (const u8 *)&a->key + a->range.start;
  806. const u8 *b_ = (const u8 *)&b->key + b->range.start;
  807. return (a->range.end == b->range.end)
  808. && (a->range.start == b->range.start)
  809. && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
  810. }
  811. static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
  812. const struct sw_flow_mask *mask)
  813. {
  814. struct mask_array *ma;
  815. int i;
  816. ma = ovsl_dereference(tbl->mask_array);
  817. for (i = 0; i < ma->max; i++) {
  818. struct sw_flow_mask *t;
  819. t = ovsl_dereference(ma->masks[i]);
  820. if (t && mask_equal(mask, t))
  821. return t;
  822. }
  823. return NULL;
  824. }
  825. /* Add 'mask' into the mask list, if it is not already there. */
  826. static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
  827. const struct sw_flow_mask *new)
  828. {
  829. struct sw_flow_mask *mask;
  830. mask = flow_mask_find(tbl, new);
  831. if (!mask) {
  832. /* Allocate a new mask if none exsits. */
  833. mask = mask_alloc();
  834. if (!mask)
  835. return -ENOMEM;
  836. mask->key = new->key;
  837. mask->range = new->range;
  838. /* Add mask to mask-list. */
  839. if (tbl_mask_array_add_mask(tbl, mask)) {
  840. kfree(mask);
  841. return -ENOMEM;
  842. }
  843. } else {
  844. BUG_ON(!mask->ref_count);
  845. mask->ref_count++;
  846. }
  847. flow->mask = mask;
  848. return 0;
  849. }
  850. /* Must be called with OVS mutex held. */
  851. static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
  852. {
  853. struct table_instance *new_ti = NULL;
  854. struct table_instance *ti;
  855. flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
  856. ti = ovsl_dereference(table->ti);
  857. table_instance_insert(ti, flow);
  858. table->count++;
  859. /* Expand table, if necessary, to make room. */
  860. if (table->count > ti->n_buckets)
  861. new_ti = table_instance_expand(ti, false);
  862. else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
  863. new_ti = table_instance_rehash(ti, ti->n_buckets, false);
  864. if (new_ti) {
  865. rcu_assign_pointer(table->ti, new_ti);
  866. call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
  867. table->last_rehash = jiffies;
  868. }
  869. }
  870. /* Must be called with OVS mutex held. */
  871. static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
  872. {
  873. struct table_instance *ti;
  874. flow->ufid_table.hash = ufid_hash(&flow->id);
  875. ti = ovsl_dereference(table->ufid_ti);
  876. ufid_table_instance_insert(ti, flow);
  877. table->ufid_count++;
  878. /* Expand table, if necessary, to make room. */
  879. if (table->ufid_count > ti->n_buckets) {
  880. struct table_instance *new_ti;
  881. new_ti = table_instance_expand(ti, true);
  882. if (new_ti) {
  883. rcu_assign_pointer(table->ufid_ti, new_ti);
  884. call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
  885. }
  886. }
  887. }
  888. /* Must be called with OVS mutex held. */
  889. int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
  890. const struct sw_flow_mask *mask)
  891. {
  892. int err;
  893. err = flow_mask_insert(table, flow, mask);
  894. if (err)
  895. return err;
  896. flow_key_insert(table, flow);
  897. if (ovs_identifier_is_ufid(&flow->id))
  898. flow_ufid_insert(table, flow);
  899. return 0;
  900. }
  901. static int compare_mask_and_count(const void *a, const void *b)
  902. {
  903. const struct mask_count *mc_a = a;
  904. const struct mask_count *mc_b = b;
  905. return (s64)mc_b->counter - (s64)mc_a->counter;
  906. }
  907. /* Must be called with OVS mutex held. */
  908. void ovs_flow_masks_rebalance(struct flow_table *table)
  909. {
  910. struct mask_array *ma = rcu_dereference_ovsl(table->mask_array);
  911. struct mask_count *masks_and_count;
  912. struct mask_array *new;
  913. int masks_entries = 0;
  914. int i;
  915. /* Build array of all current entries with use counters. */
  916. masks_and_count = kmalloc_array(ma->max, sizeof(*masks_and_count),
  917. GFP_KERNEL);
  918. if (!masks_and_count)
  919. return;
  920. for (i = 0; i < ma->max; i++) {
  921. struct sw_flow_mask *mask;
  922. int cpu;
  923. mask = rcu_dereference_ovsl(ma->masks[i]);
  924. if (unlikely(!mask))
  925. break;
  926. masks_and_count[i].index = i;
  927. masks_and_count[i].counter = 0;
  928. for_each_possible_cpu(cpu) {
  929. struct mask_array_stats *stats;
  930. unsigned int start;
  931. u64 counter;
  932. stats = per_cpu_ptr(ma->masks_usage_stats, cpu);
  933. do {
  934. start = u64_stats_fetch_begin_irq(&stats->syncp);
  935. counter = stats->usage_cntrs[i];
  936. } while (u64_stats_fetch_retry_irq(&stats->syncp,
  937. start));
  938. masks_and_count[i].counter += counter;
  939. }
  940. /* Subtract the zero count value. */
  941. masks_and_count[i].counter -= ma->masks_usage_zero_cntr[i];
  942. /* Rather than calling tbl_mask_array_reset_counters()
  943. * below when no change is needed, do it inline here.
  944. */
  945. ma->masks_usage_zero_cntr[i] += masks_and_count[i].counter;
  946. }
  947. if (i == 0)
  948. goto free_mask_entries;
  949. /* Sort the entries */
  950. masks_entries = i;
  951. sort(masks_and_count, masks_entries, sizeof(*masks_and_count),
  952. compare_mask_and_count, NULL);
  953. /* If the order is the same, nothing to do... */
  954. for (i = 0; i < masks_entries; i++) {
  955. if (i != masks_and_count[i].index)
  956. break;
  957. }
  958. if (i == masks_entries)
  959. goto free_mask_entries;
  960. /* Rebuilt the new list in order of usage. */
  961. new = tbl_mask_array_alloc(ma->max);
  962. if (!new)
  963. goto free_mask_entries;
  964. for (i = 0; i < masks_entries; i++) {
  965. int index = masks_and_count[i].index;
  966. if (ovsl_dereference(ma->masks[index]))
  967. new->masks[new->count++] = ma->masks[index];
  968. }
  969. rcu_assign_pointer(table->mask_array, new);
  970. call_rcu(&ma->rcu, mask_array_rcu_cb);
  971. free_mask_entries:
  972. kfree(masks_and_count);
  973. }
  974. /* Initializes the flow module.
  975. * Returns zero if successful or a negative error code. */
  976. int ovs_flow_init(void)
  977. {
  978. BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
  979. BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
  980. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
  981. + (nr_cpu_ids
  982. * sizeof(struct sw_flow_stats *)),
  983. 0, 0, NULL);
  984. if (flow_cache == NULL)
  985. return -ENOMEM;
  986. flow_stats_cache
  987. = kmem_cache_create("sw_flow_stats", sizeof(struct sw_flow_stats),
  988. 0, SLAB_HWCACHE_ALIGN, NULL);
  989. if (flow_stats_cache == NULL) {
  990. kmem_cache_destroy(flow_cache);
  991. flow_cache = NULL;
  992. return -ENOMEM;
  993. }
  994. return 0;
  995. }
  996. /* Uninitializes the flow module. */
  997. void ovs_flow_exit(void)
  998. {
  999. kmem_cache_destroy(flow_stats_cache);
  1000. kmem_cache_destroy(flow_cache);
  1001. }