flow_table.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * Copyright (c) 2007-2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #include "flow.h"
  19. #include "datapath.h"
  20. #include "flow_netlink.h"
  21. #include <linux/uaccess.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <net/llc_pdu.h>
  27. #include <linux/kernel.h>
  28. #include <linux/jhash.h>
  29. #include <linux/jiffies.h>
  30. #include <linux/llc.h>
  31. #include <linux/module.h>
  32. #include <linux/in.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/cpumask.h>
  35. #include <linux/if_arp.h>
  36. #include <linux/ip.h>
  37. #include <linux/ipv6.h>
  38. #include <linux/sctp.h>
  39. #include <linux/tcp.h>
  40. #include <linux/udp.h>
  41. #include <linux/icmp.h>
  42. #include <linux/icmpv6.h>
  43. #include <linux/rculist.h>
  44. #include <net/ip.h>
  45. #include <net/ipv6.h>
  46. #include <net/ndisc.h>
  47. #define TBL_MIN_BUCKETS 1024
  48. #define REHASH_INTERVAL (10 * 60 * HZ)
  49. static struct kmem_cache *flow_cache;
  50. struct kmem_cache *flow_stats_cache __read_mostly;
  51. static u16 range_n_bytes(const struct sw_flow_key_range *range)
  52. {
  53. return range->end - range->start;
  54. }
  55. void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
  56. bool full, const struct sw_flow_mask *mask)
  57. {
  58. int start = full ? 0 : mask->range.start;
  59. int len = full ? sizeof *dst : range_n_bytes(&mask->range);
  60. const long *m = (const long *)((const u8 *)&mask->key + start);
  61. const long *s = (const long *)((const u8 *)src + start);
  62. long *d = (long *)((u8 *)dst + start);
  63. int i;
  64. /* If 'full' is true then all of 'dst' is fully initialized. Otherwise,
  65. * if 'full' is false the memory outside of the 'mask->range' is left
  66. * uninitialized. This can be used as an optimization when further
  67. * operations on 'dst' only use contents within 'mask->range'.
  68. */
  69. for (i = 0; i < len; i += sizeof(long))
  70. *d++ = *s++ & *m++;
  71. }
  72. struct sw_flow *ovs_flow_alloc(void)
  73. {
  74. struct sw_flow *flow;
  75. struct flow_stats *stats;
  76. flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL);
  77. if (!flow)
  78. return ERR_PTR(-ENOMEM);
  79. flow->stats_last_writer = -1;
  80. /* Initialize the default stat node. */
  81. stats = kmem_cache_alloc_node(flow_stats_cache,
  82. GFP_KERNEL | __GFP_ZERO,
  83. node_online(0) ? 0 : NUMA_NO_NODE);
  84. if (!stats)
  85. goto err;
  86. spin_lock_init(&stats->lock);
  87. RCU_INIT_POINTER(flow->stats[0], stats);
  88. return flow;
  89. err:
  90. kmem_cache_free(flow_cache, flow);
  91. return ERR_PTR(-ENOMEM);
  92. }
  93. int ovs_flow_tbl_count(const struct flow_table *table)
  94. {
  95. return table->count;
  96. }
  97. static struct flex_array *alloc_buckets(unsigned int n_buckets)
  98. {
  99. struct flex_array *buckets;
  100. int i, err;
  101. buckets = flex_array_alloc(sizeof(struct hlist_head),
  102. n_buckets, GFP_KERNEL);
  103. if (!buckets)
  104. return NULL;
  105. err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
  106. if (err) {
  107. flex_array_free(buckets);
  108. return NULL;
  109. }
  110. for (i = 0; i < n_buckets; i++)
  111. INIT_HLIST_HEAD((struct hlist_head *)
  112. flex_array_get(buckets, i));
  113. return buckets;
  114. }
  115. static void flow_free(struct sw_flow *flow)
  116. {
  117. int cpu;
  118. if (ovs_identifier_is_key(&flow->id))
  119. kfree(flow->id.unmasked_key);
  120. if (flow->sf_acts)
  121. ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts);
  122. /* We open code this to make sure cpu 0 is always considered */
  123. for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpu_possible_mask))
  124. if (flow->stats[cpu])
  125. kmem_cache_free(flow_stats_cache,
  126. (struct flow_stats __force *)flow->stats[cpu]);
  127. kmem_cache_free(flow_cache, flow);
  128. }
  129. static void rcu_free_flow_callback(struct rcu_head *rcu)
  130. {
  131. struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
  132. flow_free(flow);
  133. }
  134. void ovs_flow_free(struct sw_flow *flow, bool deferred)
  135. {
  136. if (!flow)
  137. return;
  138. if (deferred)
  139. call_rcu(&flow->rcu, rcu_free_flow_callback);
  140. else
  141. flow_free(flow);
  142. }
  143. static void free_buckets(struct flex_array *buckets)
  144. {
  145. flex_array_free(buckets);
  146. }
  147. static void __table_instance_destroy(struct table_instance *ti)
  148. {
  149. free_buckets(ti->buckets);
  150. kfree(ti);
  151. }
  152. static struct table_instance *table_instance_alloc(int new_size)
  153. {
  154. struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
  155. if (!ti)
  156. return NULL;
  157. ti->buckets = alloc_buckets(new_size);
  158. if (!ti->buckets) {
  159. kfree(ti);
  160. return NULL;
  161. }
  162. ti->n_buckets = new_size;
  163. ti->node_ver = 0;
  164. ti->keep_flows = false;
  165. get_random_bytes(&ti->hash_seed, sizeof(u32));
  166. return ti;
  167. }
  168. int ovs_flow_tbl_init(struct flow_table *table)
  169. {
  170. struct table_instance *ti, *ufid_ti;
  171. ti = table_instance_alloc(TBL_MIN_BUCKETS);
  172. if (!ti)
  173. return -ENOMEM;
  174. ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
  175. if (!ufid_ti)
  176. goto free_ti;
  177. rcu_assign_pointer(table->ti, ti);
  178. rcu_assign_pointer(table->ufid_ti, ufid_ti);
  179. INIT_LIST_HEAD(&table->mask_list);
  180. table->last_rehash = jiffies;
  181. table->count = 0;
  182. table->ufid_count = 0;
  183. return 0;
  184. free_ti:
  185. __table_instance_destroy(ti);
  186. return -ENOMEM;
  187. }
  188. static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
  189. {
  190. struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
  191. __table_instance_destroy(ti);
  192. }
  193. static void table_instance_destroy(struct table_instance *ti,
  194. struct table_instance *ufid_ti,
  195. bool deferred)
  196. {
  197. int i;
  198. if (!ti)
  199. return;
  200. BUG_ON(!ufid_ti);
  201. if (ti->keep_flows)
  202. goto skip_flows;
  203. for (i = 0; i < ti->n_buckets; i++) {
  204. struct sw_flow *flow;
  205. struct hlist_head *head = flex_array_get(ti->buckets, i);
  206. struct hlist_node *n;
  207. int ver = ti->node_ver;
  208. int ufid_ver = ufid_ti->node_ver;
  209. hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) {
  210. hlist_del_rcu(&flow->flow_table.node[ver]);
  211. if (ovs_identifier_is_ufid(&flow->id))
  212. hlist_del_rcu(&flow->ufid_table.node[ufid_ver]);
  213. ovs_flow_free(flow, deferred);
  214. }
  215. }
  216. skip_flows:
  217. if (deferred) {
  218. call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
  219. call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb);
  220. } else {
  221. __table_instance_destroy(ti);
  222. __table_instance_destroy(ufid_ti);
  223. }
  224. }
  225. /* No need for locking this function is called from RCU callback or
  226. * error path.
  227. */
  228. void ovs_flow_tbl_destroy(struct flow_table *table)
  229. {
  230. struct table_instance *ti = rcu_dereference_raw(table->ti);
  231. struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti);
  232. table_instance_destroy(ti, ufid_ti, false);
  233. }
  234. struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
  235. u32 *bucket, u32 *last)
  236. {
  237. struct sw_flow *flow;
  238. struct hlist_head *head;
  239. int ver;
  240. int i;
  241. ver = ti->node_ver;
  242. while (*bucket < ti->n_buckets) {
  243. i = 0;
  244. head = flex_array_get(ti->buckets, *bucket);
  245. hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) {
  246. if (i < *last) {
  247. i++;
  248. continue;
  249. }
  250. *last = i + 1;
  251. return flow;
  252. }
  253. (*bucket)++;
  254. *last = 0;
  255. }
  256. return NULL;
  257. }
  258. static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
  259. {
  260. hash = jhash_1word(hash, ti->hash_seed);
  261. return flex_array_get(ti->buckets,
  262. (hash & (ti->n_buckets - 1)));
  263. }
  264. static void table_instance_insert(struct table_instance *ti,
  265. struct sw_flow *flow)
  266. {
  267. struct hlist_head *head;
  268. head = find_bucket(ti, flow->flow_table.hash);
  269. hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head);
  270. }
  271. static void ufid_table_instance_insert(struct table_instance *ti,
  272. struct sw_flow *flow)
  273. {
  274. struct hlist_head *head;
  275. head = find_bucket(ti, flow->ufid_table.hash);
  276. hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head);
  277. }
  278. static void flow_table_copy_flows(struct table_instance *old,
  279. struct table_instance *new, bool ufid)
  280. {
  281. int old_ver;
  282. int i;
  283. old_ver = old->node_ver;
  284. new->node_ver = !old_ver;
  285. /* Insert in new table. */
  286. for (i = 0; i < old->n_buckets; i++) {
  287. struct sw_flow *flow;
  288. struct hlist_head *head;
  289. head = flex_array_get(old->buckets, i);
  290. if (ufid)
  291. hlist_for_each_entry(flow, head,
  292. ufid_table.node[old_ver])
  293. ufid_table_instance_insert(new, flow);
  294. else
  295. hlist_for_each_entry(flow, head,
  296. flow_table.node[old_ver])
  297. table_instance_insert(new, flow);
  298. }
  299. old->keep_flows = true;
  300. }
  301. static struct table_instance *table_instance_rehash(struct table_instance *ti,
  302. int n_buckets, bool ufid)
  303. {
  304. struct table_instance *new_ti;
  305. new_ti = table_instance_alloc(n_buckets);
  306. if (!new_ti)
  307. return NULL;
  308. flow_table_copy_flows(ti, new_ti, ufid);
  309. return new_ti;
  310. }
  311. int ovs_flow_tbl_flush(struct flow_table *flow_table)
  312. {
  313. struct table_instance *old_ti, *new_ti;
  314. struct table_instance *old_ufid_ti, *new_ufid_ti;
  315. new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
  316. if (!new_ti)
  317. return -ENOMEM;
  318. new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS);
  319. if (!new_ufid_ti)
  320. goto err_free_ti;
  321. old_ti = ovsl_dereference(flow_table->ti);
  322. old_ufid_ti = ovsl_dereference(flow_table->ufid_ti);
  323. rcu_assign_pointer(flow_table->ti, new_ti);
  324. rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti);
  325. flow_table->last_rehash = jiffies;
  326. flow_table->count = 0;
  327. flow_table->ufid_count = 0;
  328. table_instance_destroy(old_ti, old_ufid_ti, true);
  329. return 0;
  330. err_free_ti:
  331. __table_instance_destroy(new_ti);
  332. return -ENOMEM;
  333. }
  334. static u32 flow_hash(const struct sw_flow_key *key,
  335. const struct sw_flow_key_range *range)
  336. {
  337. int key_start = range->start;
  338. int key_end = range->end;
  339. const u32 *hash_key = (const u32 *)((const u8 *)key + key_start);
  340. int hash_u32s = (key_end - key_start) >> 2;
  341. /* Make sure number of hash bytes are multiple of u32. */
  342. BUILD_BUG_ON(sizeof(long) % sizeof(u32));
  343. return jhash2(hash_key, hash_u32s, 0);
  344. }
  345. static int flow_key_start(const struct sw_flow_key *key)
  346. {
  347. if (key->tun_proto)
  348. return 0;
  349. else
  350. return rounddown(offsetof(struct sw_flow_key, phy),
  351. sizeof(long));
  352. }
  353. static bool cmp_key(const struct sw_flow_key *key1,
  354. const struct sw_flow_key *key2,
  355. int key_start, int key_end)
  356. {
  357. const long *cp1 = (const long *)((const u8 *)key1 + key_start);
  358. const long *cp2 = (const long *)((const u8 *)key2 + key_start);
  359. long diffs = 0;
  360. int i;
  361. for (i = key_start; i < key_end; i += sizeof(long))
  362. diffs |= *cp1++ ^ *cp2++;
  363. return diffs == 0;
  364. }
  365. static bool flow_cmp_masked_key(const struct sw_flow *flow,
  366. const struct sw_flow_key *key,
  367. const struct sw_flow_key_range *range)
  368. {
  369. return cmp_key(&flow->key, key, range->start, range->end);
  370. }
  371. static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
  372. const struct sw_flow_match *match)
  373. {
  374. struct sw_flow_key *key = match->key;
  375. int key_start = flow_key_start(key);
  376. int key_end = match->range.end;
  377. BUG_ON(ovs_identifier_is_ufid(&flow->id));
  378. return cmp_key(flow->id.unmasked_key, key, key_start, key_end);
  379. }
  380. static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
  381. const struct sw_flow_key *unmasked,
  382. const struct sw_flow_mask *mask)
  383. {
  384. struct sw_flow *flow;
  385. struct hlist_head *head;
  386. u32 hash;
  387. struct sw_flow_key masked_key;
  388. ovs_flow_mask_key(&masked_key, unmasked, false, mask);
  389. hash = flow_hash(&masked_key, &mask->range);
  390. head = find_bucket(ti, hash);
  391. hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) {
  392. if (flow->mask == mask && flow->flow_table.hash == hash &&
  393. flow_cmp_masked_key(flow, &masked_key, &mask->range))
  394. return flow;
  395. }
  396. return NULL;
  397. }
  398. struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
  399. const struct sw_flow_key *key,
  400. u32 *n_mask_hit)
  401. {
  402. struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
  403. struct sw_flow_mask *mask;
  404. struct sw_flow *flow;
  405. *n_mask_hit = 0;
  406. list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
  407. (*n_mask_hit)++;
  408. flow = masked_flow_lookup(ti, key, mask);
  409. if (flow) /* Found */
  410. return flow;
  411. }
  412. return NULL;
  413. }
  414. struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
  415. const struct sw_flow_key *key)
  416. {
  417. u32 __always_unused n_mask_hit;
  418. return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
  419. }
  420. struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl,
  421. const struct sw_flow_match *match)
  422. {
  423. struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
  424. struct sw_flow_mask *mask;
  425. struct sw_flow *flow;
  426. /* Always called under ovs-mutex. */
  427. list_for_each_entry(mask, &tbl->mask_list, list) {
  428. flow = masked_flow_lookup(ti, match->key, mask);
  429. if (flow && ovs_identifier_is_key(&flow->id) &&
  430. ovs_flow_cmp_unmasked_key(flow, match))
  431. return flow;
  432. }
  433. return NULL;
  434. }
  435. static u32 ufid_hash(const struct sw_flow_id *sfid)
  436. {
  437. return jhash(sfid->ufid, sfid->ufid_len, 0);
  438. }
  439. static bool ovs_flow_cmp_ufid(const struct sw_flow *flow,
  440. const struct sw_flow_id *sfid)
  441. {
  442. if (flow->id.ufid_len != sfid->ufid_len)
  443. return false;
  444. return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len);
  445. }
  446. bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match)
  447. {
  448. if (ovs_identifier_is_ufid(&flow->id))
  449. return flow_cmp_masked_key(flow, match->key, &match->range);
  450. return ovs_flow_cmp_unmasked_key(flow, match);
  451. }
  452. struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl,
  453. const struct sw_flow_id *ufid)
  454. {
  455. struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti);
  456. struct sw_flow *flow;
  457. struct hlist_head *head;
  458. u32 hash;
  459. hash = ufid_hash(ufid);
  460. head = find_bucket(ti, hash);
  461. hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) {
  462. if (flow->ufid_table.hash == hash &&
  463. ovs_flow_cmp_ufid(flow, ufid))
  464. return flow;
  465. }
  466. return NULL;
  467. }
  468. int ovs_flow_tbl_num_masks(const struct flow_table *table)
  469. {
  470. struct sw_flow_mask *mask;
  471. int num = 0;
  472. list_for_each_entry(mask, &table->mask_list, list)
  473. num++;
  474. return num;
  475. }
  476. static struct table_instance *table_instance_expand(struct table_instance *ti,
  477. bool ufid)
  478. {
  479. return table_instance_rehash(ti, ti->n_buckets * 2, ufid);
  480. }
  481. /* Remove 'mask' from the mask list, if it is not needed any more. */
  482. static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask)
  483. {
  484. if (mask) {
  485. /* ovs-lock is required to protect mask-refcount and
  486. * mask list.
  487. */
  488. ASSERT_OVSL();
  489. BUG_ON(!mask->ref_count);
  490. mask->ref_count--;
  491. if (!mask->ref_count) {
  492. list_del_rcu(&mask->list);
  493. kfree_rcu(mask, rcu);
  494. }
  495. }
  496. }
  497. /* Must be called with OVS mutex held. */
  498. void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
  499. {
  500. struct table_instance *ti = ovsl_dereference(table->ti);
  501. struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti);
  502. BUG_ON(table->count == 0);
  503. hlist_del_rcu(&flow->flow_table.node[ti->node_ver]);
  504. table->count--;
  505. if (ovs_identifier_is_ufid(&flow->id)) {
  506. hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]);
  507. table->ufid_count--;
  508. }
  509. /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be
  510. * accessible as long as the RCU read lock is held.
  511. */
  512. flow_mask_remove(table, flow->mask);
  513. }
  514. static struct sw_flow_mask *mask_alloc(void)
  515. {
  516. struct sw_flow_mask *mask;
  517. mask = kmalloc(sizeof(*mask), GFP_KERNEL);
  518. if (mask)
  519. mask->ref_count = 1;
  520. return mask;
  521. }
  522. static bool mask_equal(const struct sw_flow_mask *a,
  523. const struct sw_flow_mask *b)
  524. {
  525. const u8 *a_ = (const u8 *)&a->key + a->range.start;
  526. const u8 *b_ = (const u8 *)&b->key + b->range.start;
  527. return (a->range.end == b->range.end)
  528. && (a->range.start == b->range.start)
  529. && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
  530. }
  531. static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
  532. const struct sw_flow_mask *mask)
  533. {
  534. struct list_head *ml;
  535. list_for_each(ml, &tbl->mask_list) {
  536. struct sw_flow_mask *m;
  537. m = container_of(ml, struct sw_flow_mask, list);
  538. if (mask_equal(mask, m))
  539. return m;
  540. }
  541. return NULL;
  542. }
  543. /* Add 'mask' into the mask list, if it is not already there. */
  544. static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
  545. const struct sw_flow_mask *new)
  546. {
  547. struct sw_flow_mask *mask;
  548. mask = flow_mask_find(tbl, new);
  549. if (!mask) {
  550. /* Allocate a new mask if none exsits. */
  551. mask = mask_alloc();
  552. if (!mask)
  553. return -ENOMEM;
  554. mask->key = new->key;
  555. mask->range = new->range;
  556. list_add_rcu(&mask->list, &tbl->mask_list);
  557. } else {
  558. BUG_ON(!mask->ref_count);
  559. mask->ref_count++;
  560. }
  561. flow->mask = mask;
  562. return 0;
  563. }
  564. /* Must be called with OVS mutex held. */
  565. static void flow_key_insert(struct flow_table *table, struct sw_flow *flow)
  566. {
  567. struct table_instance *new_ti = NULL;
  568. struct table_instance *ti;
  569. flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range);
  570. ti = ovsl_dereference(table->ti);
  571. table_instance_insert(ti, flow);
  572. table->count++;
  573. /* Expand table, if necessary, to make room. */
  574. if (table->count > ti->n_buckets)
  575. new_ti = table_instance_expand(ti, false);
  576. else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
  577. new_ti = table_instance_rehash(ti, ti->n_buckets, false);
  578. if (new_ti) {
  579. rcu_assign_pointer(table->ti, new_ti);
  580. call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
  581. table->last_rehash = jiffies;
  582. }
  583. }
  584. /* Must be called with OVS mutex held. */
  585. static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow)
  586. {
  587. struct table_instance *ti;
  588. flow->ufid_table.hash = ufid_hash(&flow->id);
  589. ti = ovsl_dereference(table->ufid_ti);
  590. ufid_table_instance_insert(ti, flow);
  591. table->ufid_count++;
  592. /* Expand table, if necessary, to make room. */
  593. if (table->ufid_count > ti->n_buckets) {
  594. struct table_instance *new_ti;
  595. new_ti = table_instance_expand(ti, true);
  596. if (new_ti) {
  597. rcu_assign_pointer(table->ufid_ti, new_ti);
  598. call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
  599. }
  600. }
  601. }
  602. /* Must be called with OVS mutex held. */
  603. int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
  604. const struct sw_flow_mask *mask)
  605. {
  606. int err;
  607. err = flow_mask_insert(table, flow, mask);
  608. if (err)
  609. return err;
  610. flow_key_insert(table, flow);
  611. if (ovs_identifier_is_ufid(&flow->id))
  612. flow_ufid_insert(table, flow);
  613. return 0;
  614. }
  615. /* Initializes the flow module.
  616. * Returns zero if successful or a negative error code. */
  617. int ovs_flow_init(void)
  618. {
  619. BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
  620. BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
  621. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow)
  622. + (nr_cpu_ids
  623. * sizeof(struct flow_stats *)),
  624. 0, 0, NULL);
  625. if (flow_cache == NULL)
  626. return -ENOMEM;
  627. flow_stats_cache
  628. = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats),
  629. 0, SLAB_HWCACHE_ALIGN, NULL);
  630. if (flow_stats_cache == NULL) {
  631. kmem_cache_destroy(flow_cache);
  632. flow_cache = NULL;
  633. return -ENOMEM;
  634. }
  635. return 0;
  636. }
  637. /* Uninitializes the flow module. */
  638. void ovs_flow_exit(void)
  639. {
  640. kmem_cache_destroy(flow_stats_cache);
  641. kmem_cache_destroy(flow_cache);
  642. }