flow_table.c 19 KB

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