flow_table.c 14 KB

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