flow_table.c 29 KB

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