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