flow_table.c 19 KB

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