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