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