flow_table.c 15 KB

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