flow_table.c 19 KB

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