Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 1 | /* |
Pravin B Shelar | 9b996e5 | 2014-05-06 18:41:20 -0700 | [diff] [blame] | 2 | * Copyright (c) 2007-2014 Nicira, Inc. |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 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 | |
| 19 | #include "flow.h" |
| 20 | #include "datapath.h" |
Thomas Graf | 34ae932 | 2015-07-21 10:44:03 +0200 | [diff] [blame] | 21 | #include "flow_netlink.h" |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 22 | #include <linux/uaccess.h> |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/etherdevice.h> |
| 25 | #include <linux/if_ether.h> |
| 26 | #include <linux/if_vlan.h> |
| 27 | #include <net/llc_pdu.h> |
| 28 | #include <linux/kernel.h> |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 29 | #include <linux/jhash.h> |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 30 | #include <linux/jiffies.h> |
| 31 | #include <linux/llc.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/in.h> |
| 34 | #include <linux/rcupdate.h> |
Thadeu Lima de Souza Cascardo | db74a33 | 2016-09-15 19:11:53 -0300 | [diff] [blame] | 35 | #include <linux/cpumask.h> |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 36 | #include <linux/if_arp.h> |
| 37 | #include <linux/ip.h> |
| 38 | #include <linux/ipv6.h> |
| 39 | #include <linux/sctp.h> |
| 40 | #include <linux/tcp.h> |
| 41 | #include <linux/udp.h> |
| 42 | #include <linux/icmp.h> |
| 43 | #include <linux/icmpv6.h> |
| 44 | #include <linux/rculist.h> |
| 45 | #include <net/ip.h> |
| 46 | #include <net/ipv6.h> |
| 47 | #include <net/ndisc.h> |
| 48 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 49 | #define TBL_MIN_BUCKETS 1024 |
| 50 | #define REHASH_INTERVAL (10 * 60 * HZ) |
| 51 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 52 | static struct kmem_cache *flow_cache; |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 53 | struct kmem_cache *flow_stats_cache __read_mostly; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 54 | |
| 55 | static u16 range_n_bytes(const struct sw_flow_key_range *range) |
| 56 | { |
| 57 | return range->end - range->start; |
| 58 | } |
| 59 | |
| 60 | void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src, |
Jesse Gross | ae5f2fb | 2015-09-21 20:21:20 -0700 | [diff] [blame] | 61 | bool full, const struct sw_flow_mask *mask) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 62 | { |
Jesse Gross | ae5f2fb | 2015-09-21 20:21:20 -0700 | [diff] [blame] | 63 | int start = full ? 0 : mask->range.start; |
| 64 | int len = full ? sizeof *dst : range_n_bytes(&mask->range); |
| 65 | const long *m = (const long *)((const u8 *)&mask->key + start); |
| 66 | const long *s = (const long *)((const u8 *)src + start); |
| 67 | long *d = (long *)((u8 *)dst + start); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 68 | int i; |
| 69 | |
Jesse Gross | ae5f2fb | 2015-09-21 20:21:20 -0700 | [diff] [blame] | 70 | /* If 'full' is true then all of 'dst' is fully initialized. Otherwise, |
| 71 | * if 'full' is false the memory outside of the 'mask->range' is left |
| 72 | * uninitialized. This can be used as an optimization when further |
| 73 | * operations on 'dst' only use contents within 'mask->range'. |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 74 | */ |
Jesse Gross | ae5f2fb | 2015-09-21 20:21:20 -0700 | [diff] [blame] | 75 | for (i = 0; i < len; i += sizeof(long)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 76 | *d++ = *s++ & *m++; |
| 77 | } |
| 78 | |
Jarno Rajahalme | 23dabf8 | 2014-03-27 12:35:23 -0700 | [diff] [blame] | 79 | struct sw_flow *ovs_flow_alloc(void) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 80 | { |
| 81 | struct sw_flow *flow; |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 82 | struct flow_stats *stats; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 83 | |
Thadeu Lima de Souza Cascardo | db74a33 | 2016-09-15 19:11:53 -0300 | [diff] [blame] | 84 | flow = kmem_cache_zalloc(flow_cache, GFP_KERNEL); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 85 | if (!flow) |
| 86 | return ERR_PTR(-ENOMEM); |
| 87 | |
Thadeu Lima de Souza Cascardo | db74a33 | 2016-09-15 19:11:53 -0300 | [diff] [blame] | 88 | flow->stats_last_writer = -1; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 89 | |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 90 | /* Initialize the default stat node. */ |
| 91 | stats = kmem_cache_alloc_node(flow_stats_cache, |
Konstantin Khlebnikov | 598c12d | 2015-10-02 13:18:22 +0300 | [diff] [blame] | 92 | GFP_KERNEL | __GFP_ZERO, |
| 93 | node_online(0) ? 0 : NUMA_NO_NODE); |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 94 | if (!stats) |
Jarno Rajahalme | 23dabf8 | 2014-03-27 12:35:23 -0700 | [diff] [blame] | 95 | goto err; |
Pravin B Shelar | e298e50 | 2013-10-29 17:22:21 -0700 | [diff] [blame] | 96 | |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 97 | spin_lock_init(&stats->lock); |
Pravin B Shelar | e298e50 | 2013-10-29 17:22:21 -0700 | [diff] [blame] | 98 | |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 99 | RCU_INIT_POINTER(flow->stats[0], stats); |
| 100 | |
Tonghao Zhang | c4b2bf6 | 2017-07-17 23:28:06 -0700 | [diff] [blame] | 101 | cpumask_set_cpu(0, &flow->cpu_used_mask); |
| 102 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 103 | return flow; |
Pravin B Shelar | e298e50 | 2013-10-29 17:22:21 -0700 | [diff] [blame] | 104 | err: |
Wei Yongjun | ece37c8 | 2014-01-08 18:13:14 +0800 | [diff] [blame] | 105 | kmem_cache_free(flow_cache, flow); |
Pravin B Shelar | e298e50 | 2013-10-29 17:22:21 -0700 | [diff] [blame] | 106 | return ERR_PTR(-ENOMEM); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 109 | int ovs_flow_tbl_count(const struct flow_table *table) |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 110 | { |
| 111 | return table->count; |
| 112 | } |
| 113 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 114 | static struct flex_array *alloc_buckets(unsigned int n_buckets) |
| 115 | { |
| 116 | struct flex_array *buckets; |
| 117 | int i, err; |
| 118 | |
| 119 | buckets = flex_array_alloc(sizeof(struct hlist_head), |
| 120 | n_buckets, GFP_KERNEL); |
| 121 | if (!buckets) |
| 122 | return NULL; |
| 123 | |
| 124 | err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); |
| 125 | if (err) { |
| 126 | flex_array_free(buckets); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | for (i = 0; i < n_buckets; i++) |
| 131 | INIT_HLIST_HEAD((struct hlist_head *) |
| 132 | flex_array_get(buckets, i)); |
| 133 | |
| 134 | return buckets; |
| 135 | } |
| 136 | |
| 137 | static void flow_free(struct sw_flow *flow) |
| 138 | { |
Thadeu Lima de Souza Cascardo | db74a33 | 2016-09-15 19:11:53 -0300 | [diff] [blame] | 139 | int cpu; |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 140 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 141 | if (ovs_identifier_is_key(&flow->id)) |
| 142 | kfree(flow->id.unmasked_key); |
Thomas Graf | 34ae932 | 2015-07-21 10:44:03 +0200 | [diff] [blame] | 143 | if (flow->sf_acts) |
| 144 | ovs_nla_free_flow_actions((struct sw_flow_actions __force *)flow->sf_acts); |
Thadeu Lima de Souza Cascardo | db74a33 | 2016-09-15 19:11:53 -0300 | [diff] [blame] | 145 | /* We open code this to make sure cpu 0 is always considered */ |
Tonghao Zhang | c4b2bf6 | 2017-07-17 23:28:06 -0700 | [diff] [blame] | 146 | for (cpu = 0; cpu < nr_cpu_ids; cpu = cpumask_next(cpu, &flow->cpu_used_mask)) |
Thadeu Lima de Souza Cascardo | db74a33 | 2016-09-15 19:11:53 -0300 | [diff] [blame] | 147 | if (flow->stats[cpu]) |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 148 | kmem_cache_free(flow_stats_cache, |
Thadeu Lima de Souza Cascardo | db74a33 | 2016-09-15 19:11:53 -0300 | [diff] [blame] | 149 | (struct flow_stats __force *)flow->stats[cpu]); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 150 | kmem_cache_free(flow_cache, flow); |
| 151 | } |
| 152 | |
| 153 | static void rcu_free_flow_callback(struct rcu_head *rcu) |
| 154 | { |
| 155 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); |
| 156 | |
| 157 | flow_free(flow); |
| 158 | } |
| 159 | |
| 160 | void ovs_flow_free(struct sw_flow *flow, bool deferred) |
| 161 | { |
| 162 | if (!flow) |
| 163 | return; |
| 164 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 165 | if (deferred) |
| 166 | call_rcu(&flow->rcu, rcu_free_flow_callback); |
| 167 | else |
| 168 | flow_free(flow); |
| 169 | } |
| 170 | |
| 171 | static void free_buckets(struct flex_array *buckets) |
| 172 | { |
| 173 | flex_array_free(buckets); |
| 174 | } |
| 175 | |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 176 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 177 | static void __table_instance_destroy(struct table_instance *ti) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 178 | { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 179 | free_buckets(ti->buckets); |
| 180 | kfree(ti); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 183 | static struct table_instance *table_instance_alloc(int new_size) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 184 | { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 185 | struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 186 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 187 | if (!ti) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 188 | return NULL; |
| 189 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 190 | ti->buckets = alloc_buckets(new_size); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 191 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 192 | if (!ti->buckets) { |
| 193 | kfree(ti); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 194 | return NULL; |
| 195 | } |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 196 | ti->n_buckets = new_size; |
| 197 | ti->node_ver = 0; |
| 198 | ti->keep_flows = false; |
| 199 | get_random_bytes(&ti->hash_seed, sizeof(u32)); |
| 200 | |
| 201 | return ti; |
| 202 | } |
| 203 | |
| 204 | int ovs_flow_tbl_init(struct flow_table *table) |
| 205 | { |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 206 | struct table_instance *ti, *ufid_ti; |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 207 | |
| 208 | ti = table_instance_alloc(TBL_MIN_BUCKETS); |
| 209 | |
| 210 | if (!ti) |
| 211 | return -ENOMEM; |
| 212 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 213 | ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
| 214 | if (!ufid_ti) |
| 215 | goto free_ti; |
| 216 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 217 | rcu_assign_pointer(table->ti, ti); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 218 | rcu_assign_pointer(table->ufid_ti, ufid_ti); |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 219 | INIT_LIST_HEAD(&table->mask_list); |
| 220 | table->last_rehash = jiffies; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 221 | table->count = 0; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 222 | table->ufid_count = 0; |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 223 | return 0; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 224 | |
| 225 | free_ti: |
| 226 | __table_instance_destroy(ti); |
| 227 | return -ENOMEM; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) |
| 231 | { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 232 | struct table_instance *ti = container_of(rcu, struct table_instance, rcu); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 233 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 234 | __table_instance_destroy(ti); |
| 235 | } |
| 236 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 237 | static void table_instance_destroy(struct table_instance *ti, |
| 238 | struct table_instance *ufid_ti, |
| 239 | bool deferred) |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 240 | { |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 241 | int i; |
| 242 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 243 | if (!ti) |
| 244 | return; |
| 245 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 246 | BUG_ON(!ufid_ti); |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 247 | if (ti->keep_flows) |
| 248 | goto skip_flows; |
| 249 | |
| 250 | for (i = 0; i < ti->n_buckets; i++) { |
| 251 | struct sw_flow *flow; |
| 252 | struct hlist_head *head = flex_array_get(ti->buckets, i); |
| 253 | struct hlist_node *n; |
| 254 | int ver = ti->node_ver; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 255 | int ufid_ver = ufid_ti->node_ver; |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 256 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 257 | hlist_for_each_entry_safe(flow, n, head, flow_table.node[ver]) { |
| 258 | hlist_del_rcu(&flow->flow_table.node[ver]); |
| 259 | if (ovs_identifier_is_ufid(&flow->id)) |
| 260 | hlist_del_rcu(&flow->ufid_table.node[ufid_ver]); |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 261 | ovs_flow_free(flow, deferred); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | skip_flows: |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 266 | if (deferred) { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 267 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 268 | call_rcu(&ufid_ti->rcu, flow_tbl_destroy_rcu_cb); |
| 269 | } else { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 270 | __table_instance_destroy(ti); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 271 | __table_instance_destroy(ufid_ti); |
| 272 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Pravin B Shelar | 9b996e5 | 2014-05-06 18:41:20 -0700 | [diff] [blame] | 275 | /* No need for locking this function is called from RCU callback or |
| 276 | * error path. |
| 277 | */ |
| 278 | void ovs_flow_tbl_destroy(struct flow_table *table) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 279 | { |
Pravin B Shelar | 9b996e5 | 2014-05-06 18:41:20 -0700 | [diff] [blame] | 280 | struct table_instance *ti = rcu_dereference_raw(table->ti); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 281 | struct table_instance *ufid_ti = rcu_dereference_raw(table->ufid_ti); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 282 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 283 | table_instance_destroy(ti, ufid_ti, false); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 286 | struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti, |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 287 | u32 *bucket, u32 *last) |
| 288 | { |
| 289 | struct sw_flow *flow; |
| 290 | struct hlist_head *head; |
| 291 | int ver; |
| 292 | int i; |
| 293 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 294 | ver = ti->node_ver; |
| 295 | while (*bucket < ti->n_buckets) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 296 | i = 0; |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 297 | head = flex_array_get(ti->buckets, *bucket); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 298 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ver]) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 299 | if (i < *last) { |
| 300 | i++; |
| 301 | continue; |
| 302 | } |
| 303 | *last = i + 1; |
| 304 | return flow; |
| 305 | } |
| 306 | (*bucket)++; |
| 307 | *last = 0; |
| 308 | } |
| 309 | |
| 310 | return NULL; |
| 311 | } |
| 312 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 313 | static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 314 | { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 315 | hash = jhash_1word(hash, ti->hash_seed); |
| 316 | return flex_array_get(ti->buckets, |
| 317 | (hash & (ti->n_buckets - 1))); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 320 | static void table_instance_insert(struct table_instance *ti, |
| 321 | struct sw_flow *flow) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 322 | { |
| 323 | struct hlist_head *head; |
| 324 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 325 | head = find_bucket(ti, flow->flow_table.hash); |
| 326 | hlist_add_head_rcu(&flow->flow_table.node[ti->node_ver], head); |
| 327 | } |
| 328 | |
| 329 | static void ufid_table_instance_insert(struct table_instance *ti, |
| 330 | struct sw_flow *flow) |
| 331 | { |
| 332 | struct hlist_head *head; |
| 333 | |
| 334 | head = find_bucket(ti, flow->ufid_table.hash); |
| 335 | hlist_add_head_rcu(&flow->ufid_table.node[ti->node_ver], head); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 338 | static void flow_table_copy_flows(struct table_instance *old, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 339 | struct table_instance *new, bool ufid) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 340 | { |
| 341 | int old_ver; |
| 342 | int i; |
| 343 | |
| 344 | old_ver = old->node_ver; |
| 345 | new->node_ver = !old_ver; |
| 346 | |
| 347 | /* Insert in new table. */ |
| 348 | for (i = 0; i < old->n_buckets; i++) { |
| 349 | struct sw_flow *flow; |
| 350 | struct hlist_head *head; |
| 351 | |
| 352 | head = flex_array_get(old->buckets, i); |
| 353 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 354 | if (ufid) |
| 355 | hlist_for_each_entry(flow, head, |
| 356 | ufid_table.node[old_ver]) |
| 357 | ufid_table_instance_insert(new, flow); |
| 358 | else |
| 359 | hlist_for_each_entry(flow, head, |
| 360 | flow_table.node[old_ver]) |
| 361 | table_instance_insert(new, flow); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 364 | old->keep_flows = true; |
| 365 | } |
| 366 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 367 | static struct table_instance *table_instance_rehash(struct table_instance *ti, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 368 | int n_buckets, bool ufid) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 369 | { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 370 | struct table_instance *new_ti; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 371 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 372 | new_ti = table_instance_alloc(n_buckets); |
| 373 | if (!new_ti) |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 374 | return NULL; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 375 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 376 | flow_table_copy_flows(ti, new_ti, ufid); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 377 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 378 | return new_ti; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 381 | int ovs_flow_tbl_flush(struct flow_table *flow_table) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 382 | { |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 383 | struct table_instance *old_ti, *new_ti; |
| 384 | struct table_instance *old_ufid_ti, *new_ufid_ti; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 385 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 386 | new_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
| 387 | if (!new_ti) |
| 388 | return -ENOMEM; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 389 | new_ufid_ti = table_instance_alloc(TBL_MIN_BUCKETS); |
| 390 | if (!new_ufid_ti) |
| 391 | goto err_free_ti; |
| 392 | |
| 393 | old_ti = ovsl_dereference(flow_table->ti); |
| 394 | old_ufid_ti = ovsl_dereference(flow_table->ufid_ti); |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 395 | |
| 396 | rcu_assign_pointer(flow_table->ti, new_ti); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 397 | rcu_assign_pointer(flow_table->ufid_ti, new_ufid_ti); |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 398 | flow_table->last_rehash = jiffies; |
| 399 | flow_table->count = 0; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 400 | flow_table->ufid_count = 0; |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 401 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 402 | table_instance_destroy(old_ti, old_ufid_ti, true); |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 403 | return 0; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 404 | |
| 405 | err_free_ti: |
| 406 | __table_instance_destroy(new_ti); |
| 407 | return -ENOMEM; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Joe Stringer | 272c2cf | 2015-01-21 16:42:50 -0800 | [diff] [blame] | 410 | static u32 flow_hash(const struct sw_flow_key *key, |
| 411 | const struct sw_flow_key_range *range) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 412 | { |
Joe Stringer | 272c2cf | 2015-01-21 16:42:50 -0800 | [diff] [blame] | 413 | int key_start = range->start; |
| 414 | int key_end = range->end; |
Daniele Di Proietto | 7085130 | 2014-01-23 10:56:49 -0800 | [diff] [blame] | 415 | const u32 *hash_key = (const u32 *)((const u8 *)key + key_start); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 416 | int hash_u32s = (key_end - key_start) >> 2; |
| 417 | |
| 418 | /* Make sure number of hash bytes are multiple of u32. */ |
| 419 | BUILD_BUG_ON(sizeof(long) % sizeof(u32)); |
| 420 | |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 421 | return jhash2(hash_key, hash_u32s, 0); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | static int flow_key_start(const struct sw_flow_key *key) |
| 425 | { |
Jiri Benc | 00a93ba | 2015-10-05 13:09:46 +0200 | [diff] [blame] | 426 | if (key->tun_proto) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 427 | return 0; |
| 428 | else |
| 429 | return rounddown(offsetof(struct sw_flow_key, phy), |
| 430 | sizeof(long)); |
| 431 | } |
| 432 | |
| 433 | static bool cmp_key(const struct sw_flow_key *key1, |
| 434 | const struct sw_flow_key *key2, |
| 435 | int key_start, int key_end) |
| 436 | { |
Daniele Di Proietto | 7085130 | 2014-01-23 10:56:49 -0800 | [diff] [blame] | 437 | const long *cp1 = (const long *)((const u8 *)key1 + key_start); |
| 438 | const long *cp2 = (const long *)((const u8 *)key2 + key_start); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 439 | long diffs = 0; |
| 440 | int i; |
| 441 | |
| 442 | for (i = key_start; i < key_end; i += sizeof(long)) |
| 443 | diffs |= *cp1++ ^ *cp2++; |
| 444 | |
| 445 | return diffs == 0; |
| 446 | } |
| 447 | |
| 448 | static bool flow_cmp_masked_key(const struct sw_flow *flow, |
| 449 | const struct sw_flow_key *key, |
Joe Stringer | 272c2cf | 2015-01-21 16:42:50 -0800 | [diff] [blame] | 450 | const struct sw_flow_key_range *range) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 451 | { |
Joe Stringer | 272c2cf | 2015-01-21 16:42:50 -0800 | [diff] [blame] | 452 | return cmp_key(&flow->key, key, range->start, range->end); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 455 | static bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, |
| 456 | const struct sw_flow_match *match) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 457 | { |
| 458 | struct sw_flow_key *key = match->key; |
| 459 | int key_start = flow_key_start(key); |
| 460 | int key_end = match->range.end; |
| 461 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 462 | BUG_ON(ovs_identifier_is_ufid(&flow->id)); |
| 463 | return cmp_key(flow->id.unmasked_key, key, key_start, key_end); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 466 | static struct sw_flow *masked_flow_lookup(struct table_instance *ti, |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 467 | const struct sw_flow_key *unmasked, |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 468 | const struct sw_flow_mask *mask) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 469 | { |
| 470 | struct sw_flow *flow; |
| 471 | struct hlist_head *head; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 472 | u32 hash; |
| 473 | struct sw_flow_key masked_key; |
| 474 | |
Jesse Gross | ae5f2fb | 2015-09-21 20:21:20 -0700 | [diff] [blame] | 475 | ovs_flow_mask_key(&masked_key, unmasked, false, mask); |
Joe Stringer | 272c2cf | 2015-01-21 16:42:50 -0800 | [diff] [blame] | 476 | hash = flow_hash(&masked_key, &mask->range); |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 477 | head = find_bucket(ti, hash); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 478 | hlist_for_each_entry_rcu(flow, head, flow_table.node[ti->node_ver]) { |
| 479 | if (flow->mask == mask && flow->flow_table.hash == hash && |
Joe Stringer | 272c2cf | 2015-01-21 16:42:50 -0800 | [diff] [blame] | 480 | flow_cmp_masked_key(flow, &masked_key, &mask->range)) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 481 | return flow; |
| 482 | } |
| 483 | return NULL; |
| 484 | } |
| 485 | |
Andy Zhou | 5bb5063 | 2013-11-25 10:42:46 -0800 | [diff] [blame] | 486 | struct sw_flow *ovs_flow_tbl_lookup_stats(struct flow_table *tbl, |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 487 | const struct sw_flow_key *key, |
| 488 | u32 *n_mask_hit) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 489 | { |
Jesse Gross | 663efa3 | 2013-12-03 10:58:53 -0800 | [diff] [blame] | 490 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 491 | struct sw_flow_mask *mask; |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 492 | struct sw_flow *flow; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 493 | |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 494 | *n_mask_hit = 0; |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 495 | list_for_each_entry_rcu(mask, &tbl->mask_list, list) { |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 496 | (*n_mask_hit)++; |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 497 | flow = masked_flow_lookup(ti, key, mask); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 498 | if (flow) /* Found */ |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 499 | return flow; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 500 | } |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 501 | return NULL; |
| 502 | } |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 503 | |
Andy Zhou | 5bb5063 | 2013-11-25 10:42:46 -0800 | [diff] [blame] | 504 | struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl, |
| 505 | const struct sw_flow_key *key) |
| 506 | { |
| 507 | u32 __always_unused n_mask_hit; |
| 508 | |
| 509 | return ovs_flow_tbl_lookup_stats(tbl, key, &n_mask_hit); |
| 510 | } |
| 511 | |
Alex Wang | 4a46b24 | 2014-06-30 20:30:29 -0700 | [diff] [blame] | 512 | struct sw_flow *ovs_flow_tbl_lookup_exact(struct flow_table *tbl, |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 513 | const struct sw_flow_match *match) |
Alex Wang | 4a46b24 | 2014-06-30 20:30:29 -0700 | [diff] [blame] | 514 | { |
| 515 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ti); |
| 516 | struct sw_flow_mask *mask; |
| 517 | struct sw_flow *flow; |
| 518 | |
| 519 | /* Always called under ovs-mutex. */ |
| 520 | list_for_each_entry(mask, &tbl->mask_list, list) { |
| 521 | flow = masked_flow_lookup(ti, match->key, mask); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 522 | if (flow && ovs_identifier_is_key(&flow->id) && |
| 523 | ovs_flow_cmp_unmasked_key(flow, match)) |
| 524 | return flow; |
| 525 | } |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | static u32 ufid_hash(const struct sw_flow_id *sfid) |
| 530 | { |
| 531 | return jhash(sfid->ufid, sfid->ufid_len, 0); |
| 532 | } |
| 533 | |
| 534 | static bool ovs_flow_cmp_ufid(const struct sw_flow *flow, |
| 535 | const struct sw_flow_id *sfid) |
| 536 | { |
| 537 | if (flow->id.ufid_len != sfid->ufid_len) |
| 538 | return false; |
| 539 | |
| 540 | return !memcmp(flow->id.ufid, sfid->ufid, sfid->ufid_len); |
| 541 | } |
| 542 | |
| 543 | bool ovs_flow_cmp(const struct sw_flow *flow, const struct sw_flow_match *match) |
| 544 | { |
| 545 | if (ovs_identifier_is_ufid(&flow->id)) |
| 546 | return flow_cmp_masked_key(flow, match->key, &match->range); |
| 547 | |
| 548 | return ovs_flow_cmp_unmasked_key(flow, match); |
| 549 | } |
| 550 | |
| 551 | struct sw_flow *ovs_flow_tbl_lookup_ufid(struct flow_table *tbl, |
| 552 | const struct sw_flow_id *ufid) |
| 553 | { |
| 554 | struct table_instance *ti = rcu_dereference_ovsl(tbl->ufid_ti); |
| 555 | struct sw_flow *flow; |
| 556 | struct hlist_head *head; |
| 557 | u32 hash; |
| 558 | |
| 559 | hash = ufid_hash(ufid); |
| 560 | head = find_bucket(ti, hash); |
| 561 | hlist_for_each_entry_rcu(flow, head, ufid_table.node[ti->node_ver]) { |
| 562 | if (flow->ufid_table.hash == hash && |
| 563 | ovs_flow_cmp_ufid(flow, ufid)) |
Alex Wang | 4a46b24 | 2014-06-30 20:30:29 -0700 | [diff] [blame] | 564 | return flow; |
| 565 | } |
| 566 | return NULL; |
| 567 | } |
| 568 | |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 569 | int ovs_flow_tbl_num_masks(const struct flow_table *table) |
| 570 | { |
| 571 | struct sw_flow_mask *mask; |
| 572 | int num = 0; |
| 573 | |
| 574 | list_for_each_entry(mask, &table->mask_list, list) |
| 575 | num++; |
| 576 | |
| 577 | return num; |
| 578 | } |
| 579 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 580 | static struct table_instance *table_instance_expand(struct table_instance *ti, |
| 581 | bool ufid) |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 582 | { |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 583 | return table_instance_rehash(ti, ti->n_buckets * 2, ufid); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Jarno Rajahalme | 56c1986 | 2014-05-05 13:24:53 -0700 | [diff] [blame] | 586 | /* Remove 'mask' from the mask list, if it is not needed any more. */ |
| 587 | static void flow_mask_remove(struct flow_table *tbl, struct sw_flow_mask *mask) |
| 588 | { |
| 589 | if (mask) { |
| 590 | /* ovs-lock is required to protect mask-refcount and |
| 591 | * mask list. |
| 592 | */ |
| 593 | ASSERT_OVSL(); |
| 594 | BUG_ON(!mask->ref_count); |
| 595 | mask->ref_count--; |
| 596 | |
| 597 | if (!mask->ref_count) { |
| 598 | list_del_rcu(&mask->list); |
| 599 | kfree_rcu(mask, rcu); |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* Must be called with OVS mutex held. */ |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 605 | void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) |
| 606 | { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 607 | struct table_instance *ti = ovsl_dereference(table->ti); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 608 | struct table_instance *ufid_ti = ovsl_dereference(table->ufid_ti); |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 609 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 610 | BUG_ON(table->count == 0); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 611 | hlist_del_rcu(&flow->flow_table.node[ti->node_ver]); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 612 | table->count--; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 613 | if (ovs_identifier_is_ufid(&flow->id)) { |
| 614 | hlist_del_rcu(&flow->ufid_table.node[ufid_ti->node_ver]); |
| 615 | table->ufid_count--; |
| 616 | } |
Jarno Rajahalme | 56c1986 | 2014-05-05 13:24:53 -0700 | [diff] [blame] | 617 | |
| 618 | /* RCU delete the mask. 'flow->mask' is not NULLed, as it should be |
| 619 | * accessible as long as the RCU read lock is held. |
| 620 | */ |
| 621 | flow_mask_remove(table, flow->mask); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 624 | static struct sw_flow_mask *mask_alloc(void) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 625 | { |
| 626 | struct sw_flow_mask *mask; |
| 627 | |
| 628 | mask = kmalloc(sizeof(*mask), GFP_KERNEL); |
| 629 | if (mask) |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 630 | mask->ref_count = 1; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 631 | |
| 632 | return mask; |
| 633 | } |
| 634 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 635 | static bool mask_equal(const struct sw_flow_mask *a, |
| 636 | const struct sw_flow_mask *b) |
| 637 | { |
Daniele Di Proietto | 7085130 | 2014-01-23 10:56:49 -0800 | [diff] [blame] | 638 | const u8 *a_ = (const u8 *)&a->key + a->range.start; |
| 639 | const u8 *b_ = (const u8 *)&b->key + b->range.start; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 640 | |
| 641 | return (a->range.end == b->range.end) |
| 642 | && (a->range.start == b->range.start) |
| 643 | && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0); |
| 644 | } |
| 645 | |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 646 | static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl, |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 647 | const struct sw_flow_mask *mask) |
| 648 | { |
| 649 | struct list_head *ml; |
| 650 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 651 | list_for_each(ml, &tbl->mask_list) { |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 652 | struct sw_flow_mask *m; |
| 653 | m = container_of(ml, struct sw_flow_mask, list); |
| 654 | if (mask_equal(mask, m)) |
| 655 | return m; |
| 656 | } |
| 657 | |
| 658 | return NULL; |
| 659 | } |
| 660 | |
Ben Pfaff | d121190 | 2013-11-25 10:40:51 -0800 | [diff] [blame] | 661 | /* Add 'mask' into the mask list, if it is not already there. */ |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 662 | static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow, |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 663 | const struct sw_flow_mask *new) |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 664 | { |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 665 | struct sw_flow_mask *mask; |
| 666 | mask = flow_mask_find(tbl, new); |
| 667 | if (!mask) { |
| 668 | /* Allocate a new mask if none exsits. */ |
| 669 | mask = mask_alloc(); |
| 670 | if (!mask) |
| 671 | return -ENOMEM; |
| 672 | mask->key = new->key; |
| 673 | mask->range = new->range; |
| 674 | list_add_rcu(&mask->list, &tbl->mask_list); |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 675 | } else { |
| 676 | BUG_ON(!mask->ref_count); |
| 677 | mask->ref_count++; |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 680 | flow->mask = mask; |
| 681 | return 0; |
| 682 | } |
| 683 | |
Jarno Rajahalme | 56c1986 | 2014-05-05 13:24:53 -0700 | [diff] [blame] | 684 | /* Must be called with OVS mutex held. */ |
Joe Stringer | d29ab6f | 2015-01-21 16:42:49 -0800 | [diff] [blame] | 685 | static void flow_key_insert(struct flow_table *table, struct sw_flow *flow) |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 686 | { |
| 687 | struct table_instance *new_ti = NULL; |
| 688 | struct table_instance *ti; |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 689 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 690 | flow->flow_table.hash = flow_hash(&flow->key, &flow->mask->range); |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 691 | ti = ovsl_dereference(table->ti); |
| 692 | table_instance_insert(ti, flow); |
| 693 | table->count++; |
| 694 | |
| 695 | /* Expand table, if necessary, to make room. */ |
| 696 | if (table->count > ti->n_buckets) |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 697 | new_ti = table_instance_expand(ti, false); |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 698 | else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL)) |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 699 | new_ti = table_instance_rehash(ti, ti->n_buckets, false); |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 700 | |
| 701 | if (new_ti) { |
| 702 | rcu_assign_pointer(table->ti, new_ti); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 703 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 704 | table->last_rehash = jiffies; |
| 705 | } |
Joe Stringer | d29ab6f | 2015-01-21 16:42:49 -0800 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /* Must be called with OVS mutex held. */ |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 709 | static void flow_ufid_insert(struct flow_table *table, struct sw_flow *flow) |
| 710 | { |
| 711 | struct table_instance *ti; |
| 712 | |
| 713 | flow->ufid_table.hash = ufid_hash(&flow->id); |
| 714 | ti = ovsl_dereference(table->ufid_ti); |
| 715 | ufid_table_instance_insert(ti, flow); |
| 716 | table->ufid_count++; |
| 717 | |
| 718 | /* Expand table, if necessary, to make room. */ |
| 719 | if (table->ufid_count > ti->n_buckets) { |
| 720 | struct table_instance *new_ti; |
| 721 | |
| 722 | new_ti = table_instance_expand(ti, true); |
| 723 | if (new_ti) { |
| 724 | rcu_assign_pointer(table->ufid_ti, new_ti); |
| 725 | call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb); |
| 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* Must be called with OVS mutex held. */ |
Joe Stringer | d29ab6f | 2015-01-21 16:42:49 -0800 | [diff] [blame] | 731 | int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, |
| 732 | const struct sw_flow_mask *mask) |
| 733 | { |
| 734 | int err; |
| 735 | |
| 736 | err = flow_mask_insert(table, flow, mask); |
| 737 | if (err) |
| 738 | return err; |
| 739 | flow_key_insert(table, flow); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 740 | if (ovs_identifier_is_ufid(&flow->id)) |
| 741 | flow_ufid_insert(table, flow); |
Joe Stringer | d29ab6f | 2015-01-21 16:42:49 -0800 | [diff] [blame] | 742 | |
Pravin B Shelar | 618ed0c | 2013-10-04 00:17:42 -0700 | [diff] [blame] | 743 | return 0; |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* Initializes the flow module. |
| 747 | * Returns zero if successful or a negative error code. */ |
| 748 | int ovs_flow_init(void) |
| 749 | { |
| 750 | BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long)); |
| 751 | BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long)); |
| 752 | |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 753 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow) |
Thadeu Lima de Souza Cascardo | db74a33 | 2016-09-15 19:11:53 -0300 | [diff] [blame] | 754 | + (nr_cpu_ids |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 755 | * sizeof(struct flow_stats *)), |
| 756 | 0, 0, NULL); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 757 | if (flow_cache == NULL) |
| 758 | return -ENOMEM; |
| 759 | |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 760 | flow_stats_cache |
| 761 | = kmem_cache_create("sw_flow_stats", sizeof(struct flow_stats), |
| 762 | 0, SLAB_HWCACHE_ALIGN, NULL); |
| 763 | if (flow_stats_cache == NULL) { |
| 764 | kmem_cache_destroy(flow_cache); |
| 765 | flow_cache = NULL; |
| 766 | return -ENOMEM; |
| 767 | } |
| 768 | |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /* Uninitializes the flow module. */ |
| 773 | void ovs_flow_exit(void) |
| 774 | { |
Jarno Rajahalme | 63e7959 | 2014-03-27 12:42:54 -0700 | [diff] [blame] | 775 | kmem_cache_destroy(flow_stats_cache); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 776 | kmem_cache_destroy(flow_cache); |
| 777 | } |