flow_table.c 13 KB

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