flow_table.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * Copyright (c) 2007-2013 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/hash.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. static u16 range_n_bytes(const struct sw_flow_key_range *range)
  49. {
  50. return range->end - range->start;
  51. }
  52. void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
  53. const struct sw_flow_mask *mask)
  54. {
  55. const long *m = (long *)((u8 *)&mask->key + mask->range.start);
  56. const long *s = (long *)((u8 *)src + mask->range.start);
  57. long *d = (long *)((u8 *)dst + mask->range.start);
  58. int i;
  59. /* The memory outside of the 'mask->range' are not set since
  60. * further operations on 'dst' only uses contents within
  61. * 'mask->range'.
  62. */
  63. for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
  64. *d++ = *s++ & *m++;
  65. }
  66. struct sw_flow *ovs_flow_alloc(bool percpu_stats)
  67. {
  68. struct sw_flow *flow;
  69. int cpu;
  70. flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
  71. if (!flow)
  72. return ERR_PTR(-ENOMEM);
  73. flow->sf_acts = NULL;
  74. flow->mask = NULL;
  75. flow->stats.is_percpu = percpu_stats;
  76. if (!percpu_stats) {
  77. flow->stats.stat = kzalloc(sizeof(*flow->stats.stat), GFP_KERNEL);
  78. if (!flow->stats.stat)
  79. goto err;
  80. spin_lock_init(&flow->stats.stat->lock);
  81. } else {
  82. flow->stats.cpu_stats = alloc_percpu(struct flow_stats);
  83. if (!flow->stats.cpu_stats)
  84. goto err;
  85. for_each_possible_cpu(cpu) {
  86. struct flow_stats *cpu_stats;
  87. cpu_stats = per_cpu_ptr(flow->stats.cpu_stats, cpu);
  88. spin_lock_init(&cpu_stats->lock);
  89. }
  90. }
  91. return flow;
  92. err:
  93. kmem_cache_free(flow_cache, flow);
  94. return ERR_PTR(-ENOMEM);
  95. }
  96. int ovs_flow_tbl_count(struct flow_table *table)
  97. {
  98. return table->count;
  99. }
  100. static struct flex_array *alloc_buckets(unsigned int n_buckets)
  101. {
  102. struct flex_array *buckets;
  103. int i, err;
  104. buckets = flex_array_alloc(sizeof(struct hlist_head),
  105. n_buckets, GFP_KERNEL);
  106. if (!buckets)
  107. return NULL;
  108. err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
  109. if (err) {
  110. flex_array_free(buckets);
  111. return NULL;
  112. }
  113. for (i = 0; i < n_buckets; i++)
  114. INIT_HLIST_HEAD((struct hlist_head *)
  115. flex_array_get(buckets, i));
  116. return buckets;
  117. }
  118. static void flow_free(struct sw_flow *flow)
  119. {
  120. kfree((struct sf_flow_acts __force *)flow->sf_acts);
  121. if (flow->stats.is_percpu)
  122. free_percpu(flow->stats.cpu_stats);
  123. else
  124. kfree(flow->stats.stat);
  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. static void flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
  133. {
  134. if (!mask)
  135. return;
  136. BUG_ON(!mask->ref_count);
  137. mask->ref_count--;
  138. if (!mask->ref_count) {
  139. list_del_rcu(&mask->list);
  140. if (deferred)
  141. kfree_rcu(mask, rcu);
  142. else
  143. kfree(mask);
  144. }
  145. }
  146. void ovs_flow_free(struct sw_flow *flow, bool deferred)
  147. {
  148. if (!flow)
  149. return;
  150. flow_mask_del_ref(flow->mask, deferred);
  151. if (deferred)
  152. call_rcu(&flow->rcu, rcu_free_flow_callback);
  153. else
  154. flow_free(flow);
  155. }
  156. static void free_buckets(struct flex_array *buckets)
  157. {
  158. flex_array_free(buckets);
  159. }
  160. static void __table_instance_destroy(struct table_instance *ti)
  161. {
  162. int i;
  163. if (ti->keep_flows)
  164. goto skip_flows;
  165. for (i = 0; i < ti->n_buckets; i++) {
  166. struct sw_flow *flow;
  167. struct hlist_head *head = flex_array_get(ti->buckets, i);
  168. struct hlist_node *n;
  169. int ver = ti->node_ver;
  170. hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
  171. hlist_del(&flow->hash_node[ver]);
  172. ovs_flow_free(flow, false);
  173. }
  174. }
  175. skip_flows:
  176. free_buckets(ti->buckets);
  177. kfree(ti);
  178. }
  179. static struct table_instance *table_instance_alloc(int new_size)
  180. {
  181. struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
  182. if (!ti)
  183. return NULL;
  184. ti->buckets = alloc_buckets(new_size);
  185. if (!ti->buckets) {
  186. kfree(ti);
  187. return NULL;
  188. }
  189. ti->n_buckets = new_size;
  190. ti->node_ver = 0;
  191. ti->keep_flows = false;
  192. get_random_bytes(&ti->hash_seed, sizeof(u32));
  193. return ti;
  194. }
  195. int ovs_flow_tbl_init(struct flow_table *table)
  196. {
  197. struct table_instance *ti;
  198. ti = table_instance_alloc(TBL_MIN_BUCKETS);
  199. if (!ti)
  200. return -ENOMEM;
  201. rcu_assign_pointer(table->ti, ti);
  202. INIT_LIST_HEAD(&table->mask_list);
  203. table->last_rehash = jiffies;
  204. table->count = 0;
  205. return 0;
  206. }
  207. static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
  208. {
  209. struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
  210. __table_instance_destroy(ti);
  211. }
  212. static void table_instance_destroy(struct table_instance *ti, bool deferred)
  213. {
  214. if (!ti)
  215. return;
  216. if (deferred)
  217. call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
  218. else
  219. __table_instance_destroy(ti);
  220. }
  221. void ovs_flow_tbl_destroy(struct flow_table *table)
  222. {
  223. struct table_instance *ti = ovsl_dereference(table->ti);
  224. table_instance_destroy(ti, false);
  225. }
  226. struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
  227. u32 *bucket, u32 *last)
  228. {
  229. struct sw_flow *flow;
  230. struct hlist_head *head;
  231. int ver;
  232. int i;
  233. ver = ti->node_ver;
  234. while (*bucket < ti->n_buckets) {
  235. i = 0;
  236. head = flex_array_get(ti->buckets, *bucket);
  237. hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
  238. if (i < *last) {
  239. i++;
  240. continue;
  241. }
  242. *last = i + 1;
  243. return flow;
  244. }
  245. (*bucket)++;
  246. *last = 0;
  247. }
  248. return NULL;
  249. }
  250. static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
  251. {
  252. hash = jhash_1word(hash, ti->hash_seed);
  253. return flex_array_get(ti->buckets,
  254. (hash & (ti->n_buckets - 1)));
  255. }
  256. static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
  257. {
  258. struct hlist_head *head;
  259. head = find_bucket(ti, flow->hash);
  260. hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
  261. }
  262. static void flow_table_copy_flows(struct table_instance *old,
  263. struct table_instance *new)
  264. {
  265. int old_ver;
  266. int i;
  267. old_ver = old->node_ver;
  268. new->node_ver = !old_ver;
  269. /* Insert in new table. */
  270. for (i = 0; i < old->n_buckets; i++) {
  271. struct sw_flow *flow;
  272. struct hlist_head *head;
  273. head = flex_array_get(old->buckets, i);
  274. hlist_for_each_entry(flow, head, hash_node[old_ver])
  275. table_instance_insert(new, flow);
  276. }
  277. old->keep_flows = true;
  278. }
  279. static struct table_instance *table_instance_rehash(struct table_instance *ti,
  280. int n_buckets)
  281. {
  282. struct table_instance *new_ti;
  283. new_ti = table_instance_alloc(n_buckets);
  284. if (!new_ti)
  285. return NULL;
  286. flow_table_copy_flows(ti, new_ti);
  287. return new_ti;
  288. }
  289. int ovs_flow_tbl_flush(struct flow_table *flow_table)
  290. {
  291. struct table_instance *old_ti;
  292. struct table_instance *new_ti;
  293. old_ti = ovsl_dereference(flow_table->ti);
  294. new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
  295. if (!new_ti)
  296. return -ENOMEM;
  297. rcu_assign_pointer(flow_table->ti, new_ti);
  298. flow_table->last_rehash = jiffies;
  299. flow_table->count = 0;
  300. table_instance_destroy(old_ti, true);
  301. return 0;
  302. }
  303. static u32 flow_hash(const struct sw_flow_key *key, int key_start,
  304. int key_end)
  305. {
  306. u32 *hash_key = (u32 *)((u8 *)key + key_start);
  307. int hash_u32s = (key_end - key_start) >> 2;
  308. /* Make sure number of hash bytes are multiple of u32. */
  309. BUILD_BUG_ON(sizeof(long) % sizeof(u32));
  310. return arch_fast_hash2(hash_key, hash_u32s, 0);
  311. }
  312. static int flow_key_start(const struct sw_flow_key *key)
  313. {
  314. if (key->tun_key.ipv4_dst)
  315. return 0;
  316. else
  317. return rounddown(offsetof(struct sw_flow_key, phy),
  318. sizeof(long));
  319. }
  320. static bool cmp_key(const struct sw_flow_key *key1,
  321. const struct sw_flow_key *key2,
  322. int key_start, int key_end)
  323. {
  324. const long *cp1 = (long *)((u8 *)key1 + key_start);
  325. const long *cp2 = (long *)((u8 *)key2 + key_start);
  326. long diffs = 0;
  327. int i;
  328. for (i = key_start; i < key_end; i += sizeof(long))
  329. diffs |= *cp1++ ^ *cp2++;
  330. return diffs == 0;
  331. }
  332. static bool flow_cmp_masked_key(const struct sw_flow *flow,
  333. const struct sw_flow_key *key,
  334. int key_start, int key_end)
  335. {
  336. return cmp_key(&flow->key, key, key_start, key_end);
  337. }
  338. bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
  339. struct sw_flow_match *match)
  340. {
  341. struct sw_flow_key *key = match->key;
  342. int key_start = flow_key_start(key);
  343. int key_end = match->range.end;
  344. return cmp_key(&flow->unmasked_key, key, key_start, key_end);
  345. }
  346. static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
  347. const struct sw_flow_key *unmasked,
  348. struct sw_flow_mask *mask)
  349. {
  350. struct sw_flow *flow;
  351. struct hlist_head *head;
  352. int key_start = mask->range.start;
  353. int key_end = mask->range.end;
  354. u32 hash;
  355. struct sw_flow_key masked_key;
  356. ovs_flow_mask_key(&masked_key, unmasked, mask);
  357. hash = flow_hash(&masked_key, key_start, key_end);
  358. head = find_bucket(ti, hash);
  359. hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
  360. if (flow->mask == mask && flow->hash == hash &&
  361. flow_cmp_masked_key(flow, &masked_key,
  362. key_start, key_end))
  363. return flow;
  364. }
  365. return NULL;
  366. }
  367. struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl,
  368. const struct sw_flow_key *key,
  369. u32 *n_mask_hit)
  370. {
  371. struct table_instance *ti = rcu_dereference_ovsl(tbl->ti);
  372. struct sw_flow_mask *mask;
  373. struct sw_flow *flow;
  374. *n_mask_hit = 0;
  375. list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
  376. (*n_mask_hit)++;
  377. flow = masked_flow_lookup(ti, key, mask);
  378. if (flow) /* Found */
  379. return flow;
  380. }
  381. return NULL;
  382. }
  383. struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
  384. const struct sw_flow_key *key)
  385. {
  386. u32 __always_unused n_mask_hit;
  387. return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit);
  388. }
  389. int ovs_flow_tbl_num_masks(const struct flow_table *table)
  390. {
  391. struct sw_flow_mask *mask;
  392. int num = 0;
  393. list_for_each_entry(mask, &table->mask_list, list)
  394. num++;
  395. return num;
  396. }
  397. static struct table_instance *table_instance_expand(struct table_instance *ti)
  398. {
  399. return table_instance_rehash(ti, ti->n_buckets * 2);
  400. }
  401. void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
  402. {
  403. struct table_instance *ti = ovsl_dereference(table->ti);
  404. BUG_ON(table->count == 0);
  405. hlist_del_rcu(&flow->hash_node[ti->node_ver]);
  406. table->count--;
  407. }
  408. static struct sw_flow_mask *mask_alloc(void)
  409. {
  410. struct sw_flow_mask *mask;
  411. mask = kmalloc(sizeof(*mask), GFP_KERNEL);
  412. if (mask)
  413. mask->ref_count = 0;
  414. return mask;
  415. }
  416. static void mask_add_ref(struct sw_flow_mask *mask)
  417. {
  418. mask->ref_count++;
  419. }
  420. static bool mask_equal(const struct sw_flow_mask *a,
  421. const struct sw_flow_mask *b)
  422. {
  423. u8 *a_ = (u8 *)&a->key + a->range.start;
  424. u8 *b_ = (u8 *)&b->key + b->range.start;
  425. return (a->range.end == b->range.end)
  426. && (a->range.start == b->range.start)
  427. && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
  428. }
  429. static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
  430. const struct sw_flow_mask *mask)
  431. {
  432. struct list_head *ml;
  433. list_for_each(ml, &tbl->mask_list) {
  434. struct sw_flow_mask *m;
  435. m = container_of(ml, struct sw_flow_mask, list);
  436. if (mask_equal(mask, m))
  437. return m;
  438. }
  439. return NULL;
  440. }
  441. /* Add 'mask' into the mask list, if it is not already there. */
  442. static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
  443. struct sw_flow_mask *new)
  444. {
  445. struct sw_flow_mask *mask;
  446. mask = flow_mask_find(tbl, new);
  447. if (!mask) {
  448. /* Allocate a new mask if none exsits. */
  449. mask = mask_alloc();
  450. if (!mask)
  451. return -ENOMEM;
  452. mask->key = new->key;
  453. mask->range = new->range;
  454. list_add_rcu(&mask->list, &tbl->mask_list);
  455. }
  456. mask_add_ref(mask);
  457. flow->mask = mask;
  458. return 0;
  459. }
  460. int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
  461. struct sw_flow_mask *mask)
  462. {
  463. struct table_instance *new_ti = NULL;
  464. struct table_instance *ti;
  465. int err;
  466. err = flow_mask_insert(table, flow, mask);
  467. if (err)
  468. return err;
  469. flow->hash = flow_hash(&flow->key, flow->mask->range.start,
  470. flow->mask->range.end);
  471. ti = ovsl_dereference(table->ti);
  472. table_instance_insert(ti, flow);
  473. table->count++;
  474. /* Expand table, if necessary, to make room. */
  475. if (table->count > ti->n_buckets)
  476. new_ti = table_instance_expand(ti);
  477. else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
  478. new_ti = table_instance_rehash(ti, ti->n_buckets);
  479. if (new_ti) {
  480. rcu_assign_pointer(table->ti, new_ti);
  481. table_instance_destroy(ti, true);
  482. table->last_rehash = jiffies;
  483. }
  484. return 0;
  485. }
  486. /* Initializes the flow module.
  487. * Returns zero if successful or a negative error code. */
  488. int ovs_flow_init(void)
  489. {
  490. BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
  491. BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
  492. flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
  493. 0, NULL);
  494. if (flow_cache == NULL)
  495. return -ENOMEM;
  496. return 0;
  497. }
  498. /* Uninitializes the flow module. */
  499. void ovs_flow_exit(void)
  500. {
  501. kmem_cache_destroy(flow_cache);
  502. }