flow_table.c 19 KB

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