Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1 | /* |
Raju Subramanian | caf2ee1 | 2012-05-03 18:55:23 -0700 | [diff] [blame] | 2 | * Copyright (c) 2007-2012 Nicira, Inc. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -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 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/if_arp.h> |
| 24 | #include <linux/if_vlan.h> |
| 25 | #include <linux/in.h> |
| 26 | #include <linux/ip.h> |
| 27 | #include <linux/jhash.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/time.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <linux/genetlink.h> |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/kthread.h> |
| 34 | #include <linux/mutex.h> |
| 35 | #include <linux/percpu.h> |
| 36 | #include <linux/rcupdate.h> |
| 37 | #include <linux/tcp.h> |
| 38 | #include <linux/udp.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 39 | #include <linux/ethtool.h> |
| 40 | #include <linux/wait.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 41 | #include <asm/div64.h> |
| 42 | #include <linux/highmem.h> |
| 43 | #include <linux/netfilter_bridge.h> |
| 44 | #include <linux/netfilter_ipv4.h> |
| 45 | #include <linux/inetdevice.h> |
| 46 | #include <linux/list.h> |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 47 | #include <linux/lockdep.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 48 | #include <linux/openvswitch.h> |
| 49 | #include <linux/rculist.h> |
| 50 | #include <linux/dmi.h> |
| 51 | #include <linux/workqueue.h> |
| 52 | #include <net/genetlink.h> |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 53 | #include <net/net_namespace.h> |
| 54 | #include <net/netns/generic.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 55 | |
| 56 | #include "datapath.h" |
| 57 | #include "flow.h" |
| 58 | #include "vport-internal_dev.h" |
| 59 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 60 | |
| 61 | #define REHASH_FLOW_INTERVAL (10 * 60 * HZ) |
| 62 | static void rehash_flow_table(struct work_struct *work); |
| 63 | static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table); |
| 64 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 65 | int ovs_net_id __read_mostly; |
| 66 | |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 67 | static void ovs_notify(struct sk_buff *skb, struct genl_info *info, |
| 68 | struct genl_multicast_group *grp) |
| 69 | { |
| 70 | genl_notify(skb, genl_info_net(info), info->snd_portid, |
| 71 | grp->id, info->nlhdr, GFP_KERNEL); |
| 72 | } |
| 73 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 74 | /** |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 75 | * DOC: Locking: |
| 76 | * |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 77 | * All writes e.g. Writes to device state (add/remove datapath, port, set |
| 78 | * operations on vports, etc.), Writes to other state (flow table |
| 79 | * modifications, set miscellaneous datapath parameters, etc.) are protected |
| 80 | * by ovs_lock. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 81 | * |
| 82 | * Reads are protected by RCU. |
| 83 | * |
| 84 | * There are a few special cases (mostly stats) that have their own |
| 85 | * synchronization but they nest under all of above and don't interact with |
| 86 | * each other. |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 87 | * |
| 88 | * The RTNL lock nests inside ovs_mutex. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 89 | */ |
| 90 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 91 | static DEFINE_MUTEX(ovs_mutex); |
| 92 | |
| 93 | void ovs_lock(void) |
| 94 | { |
| 95 | mutex_lock(&ovs_mutex); |
| 96 | } |
| 97 | |
| 98 | void ovs_unlock(void) |
| 99 | { |
| 100 | mutex_unlock(&ovs_mutex); |
| 101 | } |
| 102 | |
| 103 | #ifdef CONFIG_LOCKDEP |
| 104 | int lockdep_ovsl_is_held(void) |
| 105 | { |
| 106 | if (debug_locks) |
| 107 | return lockdep_is_held(&ovs_mutex); |
| 108 | else |
| 109 | return 1; |
| 110 | } |
| 111 | #endif |
| 112 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 113 | static struct vport *new_vport(const struct vport_parms *); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 114 | static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 115 | const struct dp_upcall_info *); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 116 | static int queue_userspace_packet(struct net *, int dp_ifindex, |
| 117 | struct sk_buff *, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 118 | const struct dp_upcall_info *); |
| 119 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 120 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 121 | static struct datapath *get_dp(struct net *net, int dp_ifindex) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 122 | { |
| 123 | struct datapath *dp = NULL; |
| 124 | struct net_device *dev; |
| 125 | |
| 126 | rcu_read_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 127 | dev = dev_get_by_index_rcu(net, dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 128 | if (dev) { |
| 129 | struct vport *vport = ovs_internal_dev_get_vport(dev); |
| 130 | if (vport) |
| 131 | dp = vport->dp; |
| 132 | } |
| 133 | rcu_read_unlock(); |
| 134 | |
| 135 | return dp; |
| 136 | } |
| 137 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 138 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 139 | const char *ovs_dp_name(const struct datapath *dp) |
| 140 | { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 141 | struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 142 | return vport->ops->get_name(vport); |
| 143 | } |
| 144 | |
| 145 | static int get_dpifindex(struct datapath *dp) |
| 146 | { |
| 147 | struct vport *local; |
| 148 | int ifindex; |
| 149 | |
| 150 | rcu_read_lock(); |
| 151 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 152 | local = ovs_vport_rcu(dp, OVSP_LOCAL); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 153 | if (local) |
| 154 | ifindex = local->ops->get_ifindex(local); |
| 155 | else |
| 156 | ifindex = 0; |
| 157 | |
| 158 | rcu_read_unlock(); |
| 159 | |
| 160 | return ifindex; |
| 161 | } |
| 162 | |
| 163 | static void destroy_dp_rcu(struct rcu_head *rcu) |
| 164 | { |
| 165 | struct datapath *dp = container_of(rcu, struct datapath, rcu); |
| 166 | |
| 167 | ovs_flow_tbl_destroy((__force struct flow_table *)dp->table); |
| 168 | free_percpu(dp->stats_percpu); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 169 | release_net(ovs_dp_get_net(dp)); |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 170 | kfree(dp->ports); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 171 | kfree(dp); |
| 172 | } |
| 173 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 174 | static struct hlist_head *vport_hash_bucket(const struct datapath *dp, |
| 175 | u16 port_no) |
| 176 | { |
| 177 | return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; |
| 178 | } |
| 179 | |
| 180 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) |
| 181 | { |
| 182 | struct vport *vport; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 183 | struct hlist_head *head; |
| 184 | |
| 185 | head = vport_hash_bucket(dp, port_no); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 186 | hlist_for_each_entry_rcu(vport, head, dp_hash_node) { |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 187 | if (vport->port_no == port_no) |
| 188 | return vport; |
| 189 | } |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 193 | /* Called with ovs_mutex. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 194 | static struct vport *new_vport(const struct vport_parms *parms) |
| 195 | { |
| 196 | struct vport *vport; |
| 197 | |
| 198 | vport = ovs_vport_add(parms); |
| 199 | if (!IS_ERR(vport)) { |
| 200 | struct datapath *dp = parms->dp; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 201 | struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 202 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 203 | hlist_add_head_rcu(&vport->dp_hash_node, head); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 204 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 205 | return vport; |
| 206 | } |
| 207 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 208 | void ovs_dp_detach_port(struct vport *p) |
| 209 | { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 210 | ASSERT_OVSL(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 211 | |
| 212 | /* First drop references to device. */ |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 213 | hlist_del_rcu(&p->dp_hash_node); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 214 | |
| 215 | /* Then destroy it. */ |
| 216 | ovs_vport_del(p); |
| 217 | } |
| 218 | |
| 219 | /* Must be called with rcu_read_lock. */ |
| 220 | void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb) |
| 221 | { |
| 222 | struct datapath *dp = p->dp; |
| 223 | struct sw_flow *flow; |
| 224 | struct dp_stats_percpu *stats; |
| 225 | struct sw_flow_key key; |
| 226 | u64 *stats_counter; |
| 227 | int error; |
| 228 | int key_len; |
| 229 | |
Shan Wei | 404f2f1 | 2012-11-13 09:52:25 +0800 | [diff] [blame] | 230 | stats = this_cpu_ptr(dp->stats_percpu); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 231 | |
| 232 | /* Extract flow from 'skb' into 'key'. */ |
| 233 | error = ovs_flow_extract(skb, p->port_no, &key, &key_len); |
| 234 | if (unlikely(error)) { |
| 235 | kfree_skb(skb); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | /* Look up flow. */ |
| 240 | flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len); |
| 241 | if (unlikely(!flow)) { |
| 242 | struct dp_upcall_info upcall; |
| 243 | |
| 244 | upcall.cmd = OVS_PACKET_CMD_MISS; |
| 245 | upcall.key = &key; |
| 246 | upcall.userdata = NULL; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 247 | upcall.portid = p->upcall_portid; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 248 | ovs_dp_upcall(dp, skb, &upcall); |
| 249 | consume_skb(skb); |
| 250 | stats_counter = &stats->n_missed; |
| 251 | goto out; |
| 252 | } |
| 253 | |
| 254 | OVS_CB(skb)->flow = flow; |
| 255 | |
| 256 | stats_counter = &stats->n_hit; |
| 257 | ovs_flow_used(OVS_CB(skb)->flow, skb); |
| 258 | ovs_execute_actions(dp, skb); |
| 259 | |
| 260 | out: |
| 261 | /* Update datapath statistics. */ |
| 262 | u64_stats_update_begin(&stats->sync); |
| 263 | (*stats_counter)++; |
| 264 | u64_stats_update_end(&stats->sync); |
| 265 | } |
| 266 | |
| 267 | static struct genl_family dp_packet_genl_family = { |
| 268 | .id = GENL_ID_GENERATE, |
| 269 | .hdrsize = sizeof(struct ovs_header), |
| 270 | .name = OVS_PACKET_FAMILY, |
| 271 | .version = OVS_PACKET_VERSION, |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 272 | .maxattr = OVS_PACKET_ATTR_MAX, |
Pravin B Shelar | 3a4e0d6 | 2013-04-23 07:48:48 +0000 | [diff] [blame^] | 273 | .netnsok = true, |
| 274 | .parallel_ops = true, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 278 | const struct dp_upcall_info *upcall_info) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 279 | { |
| 280 | struct dp_stats_percpu *stats; |
| 281 | int dp_ifindex; |
| 282 | int err; |
| 283 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 284 | if (upcall_info->portid == 0) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 285 | err = -ENOTCONN; |
| 286 | goto err; |
| 287 | } |
| 288 | |
| 289 | dp_ifindex = get_dpifindex(dp); |
| 290 | if (!dp_ifindex) { |
| 291 | err = -ENODEV; |
| 292 | goto err; |
| 293 | } |
| 294 | |
| 295 | if (!skb_is_gso(skb)) |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 296 | err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 297 | else |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 298 | err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 299 | if (err) |
| 300 | goto err; |
| 301 | |
| 302 | return 0; |
| 303 | |
| 304 | err: |
Shan Wei | 404f2f1 | 2012-11-13 09:52:25 +0800 | [diff] [blame] | 305 | stats = this_cpu_ptr(dp->stats_percpu); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 306 | |
| 307 | u64_stats_update_begin(&stats->sync); |
| 308 | stats->n_lost++; |
| 309 | u64_stats_update_end(&stats->sync); |
| 310 | |
| 311 | return err; |
| 312 | } |
| 313 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 314 | static int queue_gso_packets(struct net *net, int dp_ifindex, |
| 315 | struct sk_buff *skb, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 316 | const struct dp_upcall_info *upcall_info) |
| 317 | { |
Ben Pfaff | a1b5d0d | 2012-07-20 14:47:54 -0700 | [diff] [blame] | 318 | unsigned short gso_type = skb_shinfo(skb)->gso_type; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 319 | struct dp_upcall_info later_info; |
| 320 | struct sw_flow_key later_key; |
| 321 | struct sk_buff *segs, *nskb; |
| 322 | int err; |
| 323 | |
Cong Wang | 12b0004 | 2013-02-05 16:36:38 +0000 | [diff] [blame] | 324 | segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false); |
Pravin B Shelar | 92e5dfc | 2012-07-20 14:46:29 -0700 | [diff] [blame] | 325 | if (IS_ERR(segs)) |
| 326 | return PTR_ERR(segs); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 327 | |
| 328 | /* Queue all of the segments. */ |
| 329 | skb = segs; |
| 330 | do { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 331 | err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 332 | if (err) |
| 333 | break; |
| 334 | |
Ben Pfaff | a1b5d0d | 2012-07-20 14:47:54 -0700 | [diff] [blame] | 335 | if (skb == segs && gso_type & SKB_GSO_UDP) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 336 | /* The initial flow key extracted by ovs_flow_extract() |
| 337 | * in this case is for a first fragment, so we need to |
| 338 | * properly mark later fragments. |
| 339 | */ |
| 340 | later_key = *upcall_info->key; |
| 341 | later_key.ip.frag = OVS_FRAG_TYPE_LATER; |
| 342 | |
| 343 | later_info = *upcall_info; |
| 344 | later_info.key = &later_key; |
| 345 | upcall_info = &later_info; |
| 346 | } |
| 347 | } while ((skb = skb->next)); |
| 348 | |
| 349 | /* Free all of the segments. */ |
| 350 | skb = segs; |
| 351 | do { |
| 352 | nskb = skb->next; |
| 353 | if (err) |
| 354 | kfree_skb(skb); |
| 355 | else |
| 356 | consume_skb(skb); |
| 357 | } while ((skb = nskb)); |
| 358 | return err; |
| 359 | } |
| 360 | |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 361 | static size_t key_attr_size(void) |
| 362 | { |
| 363 | return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */ |
| 364 | + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */ |
| 365 | + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */ |
| 366 | + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */ |
| 367 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ |
| 368 | + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */ |
| 369 | + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */ |
| 370 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ |
| 371 | + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */ |
| 372 | + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */ |
| 373 | + nla_total_size(28); /* OVS_KEY_ATTR_ND */ |
| 374 | } |
| 375 | |
| 376 | static size_t upcall_msg_size(const struct sk_buff *skb, |
| 377 | const struct nlattr *userdata) |
| 378 | { |
| 379 | size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) |
| 380 | + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */ |
| 381 | + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */ |
| 382 | |
| 383 | /* OVS_PACKET_ATTR_USERDATA */ |
| 384 | if (userdata) |
| 385 | size += NLA_ALIGN(userdata->nla_len); |
| 386 | |
| 387 | return size; |
| 388 | } |
| 389 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 390 | static int queue_userspace_packet(struct net *net, int dp_ifindex, |
| 391 | struct sk_buff *skb, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 392 | const struct dp_upcall_info *upcall_info) |
| 393 | { |
| 394 | struct ovs_header *upcall; |
| 395 | struct sk_buff *nskb = NULL; |
| 396 | struct sk_buff *user_skb; /* to be queued to userspace */ |
| 397 | struct nlattr *nla; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 398 | int err; |
| 399 | |
| 400 | if (vlan_tx_tag_present(skb)) { |
| 401 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 402 | if (!nskb) |
| 403 | return -ENOMEM; |
| 404 | |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 405 | nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb)); |
Dan Carpenter | 8aa51d6 | 2012-05-13 08:44:18 +0000 | [diff] [blame] | 406 | if (!nskb) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 407 | return -ENOMEM; |
| 408 | |
| 409 | nskb->vlan_tci = 0; |
| 410 | skb = nskb; |
| 411 | } |
| 412 | |
| 413 | if (nla_attr_size(skb->len) > USHRT_MAX) { |
| 414 | err = -EFBIG; |
| 415 | goto out; |
| 416 | } |
| 417 | |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 418 | user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 419 | if (!user_skb) { |
| 420 | err = -ENOMEM; |
| 421 | goto out; |
| 422 | } |
| 423 | |
| 424 | upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, |
| 425 | 0, upcall_info->cmd); |
| 426 | upcall->dp_ifindex = dp_ifindex; |
| 427 | |
| 428 | nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY); |
| 429 | ovs_flow_to_nlattrs(upcall_info->key, user_skb); |
| 430 | nla_nest_end(user_skb, nla); |
| 431 | |
| 432 | if (upcall_info->userdata) |
Ben Pfaff | 4490108 | 2013-02-15 17:29:22 -0800 | [diff] [blame] | 433 | __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, |
| 434 | nla_len(upcall_info->userdata), |
| 435 | nla_data(upcall_info->userdata)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 436 | |
| 437 | nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len); |
| 438 | |
| 439 | skb_copy_and_csum_dev(skb, nla_data(nla)); |
| 440 | |
Rich Lane | a15ff76 | 2013-02-15 11:07:43 -0800 | [diff] [blame] | 441 | genlmsg_end(user_skb, upcall); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 442 | err = genlmsg_unicast(net, user_skb, upcall_info->portid); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 443 | |
| 444 | out: |
| 445 | kfree_skb(nskb); |
| 446 | return err; |
| 447 | } |
| 448 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 449 | /* Called with ovs_mutex. */ |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 450 | static int flush_flows(struct datapath *dp) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 451 | { |
| 452 | struct flow_table *old_table; |
| 453 | struct flow_table *new_table; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 454 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 455 | old_table = ovsl_dereference(dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 456 | new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS); |
| 457 | if (!new_table) |
| 458 | return -ENOMEM; |
| 459 | |
| 460 | rcu_assign_pointer(dp->table, new_table); |
| 461 | |
| 462 | ovs_flow_tbl_deferred_destroy(old_table); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static int validate_actions(const struct nlattr *attr, |
| 467 | const struct sw_flow_key *key, int depth); |
| 468 | |
| 469 | static int validate_sample(const struct nlattr *attr, |
| 470 | const struct sw_flow_key *key, int depth) |
| 471 | { |
| 472 | const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1]; |
| 473 | const struct nlattr *probability, *actions; |
| 474 | const struct nlattr *a; |
| 475 | int rem; |
| 476 | |
| 477 | memset(attrs, 0, sizeof(attrs)); |
| 478 | nla_for_each_nested(a, attr, rem) { |
| 479 | int type = nla_type(a); |
| 480 | if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type]) |
| 481 | return -EINVAL; |
| 482 | attrs[type] = a; |
| 483 | } |
| 484 | if (rem) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY]; |
| 488 | if (!probability || nla_len(probability) != sizeof(u32)) |
| 489 | return -EINVAL; |
| 490 | |
| 491 | actions = attrs[OVS_SAMPLE_ATTR_ACTIONS]; |
| 492 | if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) |
| 493 | return -EINVAL; |
| 494 | return validate_actions(actions, key, depth + 1); |
| 495 | } |
| 496 | |
Pravin B Shelar | 072ae63 | 2012-05-07 17:21:53 -0700 | [diff] [blame] | 497 | static int validate_tp_port(const struct sw_flow_key *flow_key) |
| 498 | { |
| 499 | if (flow_key->eth.type == htons(ETH_P_IP)) { |
Jesse Gross | 4185392 | 2012-08-06 15:49:47 -0700 | [diff] [blame] | 500 | if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst) |
Pravin B Shelar | 072ae63 | 2012-05-07 17:21:53 -0700 | [diff] [blame] | 501 | return 0; |
| 502 | } else if (flow_key->eth.type == htons(ETH_P_IPV6)) { |
Jesse Gross | 4185392 | 2012-08-06 15:49:47 -0700 | [diff] [blame] | 503 | if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst) |
Pravin B Shelar | 072ae63 | 2012-05-07 17:21:53 -0700 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | return -EINVAL; |
| 508 | } |
| 509 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 510 | static int validate_set(const struct nlattr *a, |
| 511 | const struct sw_flow_key *flow_key) |
| 512 | { |
| 513 | const struct nlattr *ovs_key = nla_data(a); |
| 514 | int key_type = nla_type(ovs_key); |
| 515 | |
| 516 | /* There can be only one key in a action */ |
| 517 | if (nla_total_size(nla_len(ovs_key)) != nla_len(a)) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | if (key_type > OVS_KEY_ATTR_MAX || |
| 521 | nla_len(ovs_key) != ovs_key_lens[key_type]) |
| 522 | return -EINVAL; |
| 523 | |
| 524 | switch (key_type) { |
| 525 | const struct ovs_key_ipv4 *ipv4_key; |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 526 | const struct ovs_key_ipv6 *ipv6_key; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 527 | |
| 528 | case OVS_KEY_ATTR_PRIORITY: |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 529 | case OVS_KEY_ATTR_SKB_MARK: |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 530 | case OVS_KEY_ATTR_ETHERNET: |
| 531 | break; |
| 532 | |
| 533 | case OVS_KEY_ATTR_IPV4: |
| 534 | if (flow_key->eth.type != htons(ETH_P_IP)) |
| 535 | return -EINVAL; |
| 536 | |
Jesse Gross | 4185392 | 2012-08-06 15:49:47 -0700 | [diff] [blame] | 537 | if (!flow_key->ip.proto) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 538 | return -EINVAL; |
| 539 | |
| 540 | ipv4_key = nla_data(ovs_key); |
| 541 | if (ipv4_key->ipv4_proto != flow_key->ip.proto) |
| 542 | return -EINVAL; |
| 543 | |
| 544 | if (ipv4_key->ipv4_frag != flow_key->ip.frag) |
| 545 | return -EINVAL; |
| 546 | |
| 547 | break; |
| 548 | |
Ansis Atteka | 3fdbd1c | 2012-11-13 15:44:14 -0800 | [diff] [blame] | 549 | case OVS_KEY_ATTR_IPV6: |
| 550 | if (flow_key->eth.type != htons(ETH_P_IPV6)) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | if (!flow_key->ip.proto) |
| 554 | return -EINVAL; |
| 555 | |
| 556 | ipv6_key = nla_data(ovs_key); |
| 557 | if (ipv6_key->ipv6_proto != flow_key->ip.proto) |
| 558 | return -EINVAL; |
| 559 | |
| 560 | if (ipv6_key->ipv6_frag != flow_key->ip.frag) |
| 561 | return -EINVAL; |
| 562 | |
| 563 | if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000) |
| 564 | return -EINVAL; |
| 565 | |
| 566 | break; |
| 567 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 568 | case OVS_KEY_ATTR_TCP: |
| 569 | if (flow_key->ip.proto != IPPROTO_TCP) |
| 570 | return -EINVAL; |
| 571 | |
Pravin B Shelar | 072ae63 | 2012-05-07 17:21:53 -0700 | [diff] [blame] | 572 | return validate_tp_port(flow_key); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 573 | |
| 574 | case OVS_KEY_ATTR_UDP: |
| 575 | if (flow_key->ip.proto != IPPROTO_UDP) |
| 576 | return -EINVAL; |
| 577 | |
Pravin B Shelar | 072ae63 | 2012-05-07 17:21:53 -0700 | [diff] [blame] | 578 | return validate_tp_port(flow_key); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 579 | |
| 580 | default: |
| 581 | return -EINVAL; |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int validate_userspace(const struct nlattr *attr) |
| 588 | { |
| 589 | static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = { |
| 590 | [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 }, |
Ben Pfaff | 4490108 | 2013-02-15 17:29:22 -0800 | [diff] [blame] | 591 | [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC }, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 592 | }; |
| 593 | struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1]; |
| 594 | int error; |
| 595 | |
| 596 | error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, |
| 597 | attr, userspace_policy); |
| 598 | if (error) |
| 599 | return error; |
| 600 | |
| 601 | if (!a[OVS_USERSPACE_ATTR_PID] || |
| 602 | !nla_get_u32(a[OVS_USERSPACE_ATTR_PID])) |
| 603 | return -EINVAL; |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int validate_actions(const struct nlattr *attr, |
| 609 | const struct sw_flow_key *key, int depth) |
| 610 | { |
| 611 | const struct nlattr *a; |
| 612 | int rem, err; |
| 613 | |
| 614 | if (depth >= SAMPLE_ACTION_DEPTH) |
| 615 | return -EOVERFLOW; |
| 616 | |
| 617 | nla_for_each_nested(a, attr, rem) { |
| 618 | /* Expected argument lengths, (u32)-1 for variable length. */ |
| 619 | static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = { |
| 620 | [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32), |
| 621 | [OVS_ACTION_ATTR_USERSPACE] = (u32)-1, |
| 622 | [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan), |
| 623 | [OVS_ACTION_ATTR_POP_VLAN] = 0, |
| 624 | [OVS_ACTION_ATTR_SET] = (u32)-1, |
| 625 | [OVS_ACTION_ATTR_SAMPLE] = (u32)-1 |
| 626 | }; |
| 627 | const struct ovs_action_push_vlan *vlan; |
| 628 | int type = nla_type(a); |
| 629 | |
| 630 | if (type > OVS_ACTION_ATTR_MAX || |
| 631 | (action_lens[type] != nla_len(a) && |
| 632 | action_lens[type] != (u32)-1)) |
| 633 | return -EINVAL; |
| 634 | |
| 635 | switch (type) { |
| 636 | case OVS_ACTION_ATTR_UNSPEC: |
| 637 | return -EINVAL; |
| 638 | |
| 639 | case OVS_ACTION_ATTR_USERSPACE: |
| 640 | err = validate_userspace(a); |
| 641 | if (err) |
| 642 | return err; |
| 643 | break; |
| 644 | |
| 645 | case OVS_ACTION_ATTR_OUTPUT: |
| 646 | if (nla_get_u32(a) >= DP_MAX_PORTS) |
| 647 | return -EINVAL; |
| 648 | break; |
| 649 | |
| 650 | |
| 651 | case OVS_ACTION_ATTR_POP_VLAN: |
| 652 | break; |
| 653 | |
| 654 | case OVS_ACTION_ATTR_PUSH_VLAN: |
| 655 | vlan = nla_data(a); |
| 656 | if (vlan->vlan_tpid != htons(ETH_P_8021Q)) |
| 657 | return -EINVAL; |
| 658 | if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT))) |
| 659 | return -EINVAL; |
| 660 | break; |
| 661 | |
| 662 | case OVS_ACTION_ATTR_SET: |
| 663 | err = validate_set(a, key); |
| 664 | if (err) |
| 665 | return err; |
| 666 | break; |
| 667 | |
| 668 | case OVS_ACTION_ATTR_SAMPLE: |
| 669 | err = validate_sample(a, key, depth); |
| 670 | if (err) |
| 671 | return err; |
| 672 | break; |
| 673 | |
| 674 | default: |
| 675 | return -EINVAL; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | if (rem > 0) |
| 680 | return -EINVAL; |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | static void clear_stats(struct sw_flow *flow) |
| 686 | { |
| 687 | flow->used = 0; |
| 688 | flow->tcp_flags = 0; |
| 689 | flow->packet_count = 0; |
| 690 | flow->byte_count = 0; |
| 691 | } |
| 692 | |
| 693 | static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) |
| 694 | { |
| 695 | struct ovs_header *ovs_header = info->userhdr; |
| 696 | struct nlattr **a = info->attrs; |
| 697 | struct sw_flow_actions *acts; |
| 698 | struct sk_buff *packet; |
| 699 | struct sw_flow *flow; |
| 700 | struct datapath *dp; |
| 701 | struct ethhdr *eth; |
| 702 | int len; |
| 703 | int err; |
| 704 | int key_len; |
| 705 | |
| 706 | err = -EINVAL; |
| 707 | if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || |
Thomas Graf | dded45fc | 2013-03-29 14:46:47 +0100 | [diff] [blame] | 708 | !a[OVS_PACKET_ATTR_ACTIONS]) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 709 | goto err; |
| 710 | |
| 711 | len = nla_len(a[OVS_PACKET_ATTR_PACKET]); |
| 712 | packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); |
| 713 | err = -ENOMEM; |
| 714 | if (!packet) |
| 715 | goto err; |
| 716 | skb_reserve(packet, NET_IP_ALIGN); |
| 717 | |
Thomas Graf | 32686a9 | 2013-03-29 14:46:48 +0100 | [diff] [blame] | 718 | nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 719 | |
| 720 | skb_reset_mac_header(packet); |
| 721 | eth = eth_hdr(packet); |
| 722 | |
| 723 | /* Normally, setting the skb 'protocol' field would be handled by a |
| 724 | * call to eth_type_trans(), but it assumes there's a sending |
| 725 | * device, which we may not have. */ |
Simon Horman | e5c5d22 | 2013-03-28 13:38:25 +0900 | [diff] [blame] | 726 | if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 727 | packet->protocol = eth->h_proto; |
| 728 | else |
| 729 | packet->protocol = htons(ETH_P_802_2); |
| 730 | |
| 731 | /* Build an sw_flow for sending this packet. */ |
| 732 | flow = ovs_flow_alloc(); |
| 733 | err = PTR_ERR(flow); |
| 734 | if (IS_ERR(flow)) |
| 735 | goto err_kfree_skb; |
| 736 | |
| 737 | err = ovs_flow_extract(packet, -1, &flow->key, &key_len); |
| 738 | if (err) |
| 739 | goto err_flow_free; |
| 740 | |
| 741 | err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority, |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 742 | &flow->key.phy.skb_mark, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 743 | &flow->key.phy.in_port, |
| 744 | a[OVS_PACKET_ATTR_KEY]); |
| 745 | if (err) |
| 746 | goto err_flow_free; |
| 747 | |
| 748 | err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0); |
| 749 | if (err) |
| 750 | goto err_flow_free; |
| 751 | |
| 752 | flow->hash = ovs_flow_hash(&flow->key, key_len); |
| 753 | |
| 754 | acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]); |
| 755 | err = PTR_ERR(acts); |
| 756 | if (IS_ERR(acts)) |
| 757 | goto err_flow_free; |
| 758 | rcu_assign_pointer(flow->sf_acts, acts); |
| 759 | |
| 760 | OVS_CB(packet)->flow = flow; |
| 761 | packet->priority = flow->key.phy.priority; |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 762 | packet->mark = flow->key.phy.skb_mark; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 763 | |
| 764 | rcu_read_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 765 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 766 | err = -ENODEV; |
| 767 | if (!dp) |
| 768 | goto err_unlock; |
| 769 | |
| 770 | local_bh_disable(); |
| 771 | err = ovs_execute_actions(dp, packet); |
| 772 | local_bh_enable(); |
| 773 | rcu_read_unlock(); |
| 774 | |
| 775 | ovs_flow_free(flow); |
| 776 | return err; |
| 777 | |
| 778 | err_unlock: |
| 779 | rcu_read_unlock(); |
| 780 | err_flow_free: |
| 781 | ovs_flow_free(flow); |
| 782 | err_kfree_skb: |
| 783 | kfree_skb(packet); |
| 784 | err: |
| 785 | return err; |
| 786 | } |
| 787 | |
| 788 | static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { |
Thomas Graf | dded45fc | 2013-03-29 14:46:47 +0100 | [diff] [blame] | 789 | [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 790 | [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, |
| 791 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, |
| 792 | }; |
| 793 | |
| 794 | static struct genl_ops dp_packet_genl_ops[] = { |
| 795 | { .cmd = OVS_PACKET_CMD_EXECUTE, |
| 796 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 797 | .policy = packet_policy, |
| 798 | .doit = ovs_packet_cmd_execute |
| 799 | } |
| 800 | }; |
| 801 | |
| 802 | static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats) |
| 803 | { |
| 804 | int i; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 805 | struct flow_table *table = ovsl_dereference(dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 806 | |
| 807 | stats->n_flows = ovs_flow_tbl_count(table); |
| 808 | |
| 809 | stats->n_hit = stats->n_missed = stats->n_lost = 0; |
| 810 | for_each_possible_cpu(i) { |
| 811 | const struct dp_stats_percpu *percpu_stats; |
| 812 | struct dp_stats_percpu local_stats; |
| 813 | unsigned int start; |
| 814 | |
| 815 | percpu_stats = per_cpu_ptr(dp->stats_percpu, i); |
| 816 | |
| 817 | do { |
| 818 | start = u64_stats_fetch_begin_bh(&percpu_stats->sync); |
| 819 | local_stats = *percpu_stats; |
| 820 | } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start)); |
| 821 | |
| 822 | stats->n_hit += local_stats.n_hit; |
| 823 | stats->n_missed += local_stats.n_missed; |
| 824 | stats->n_lost += local_stats.n_lost; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { |
| 829 | [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, |
| 830 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, |
| 831 | [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, |
| 832 | }; |
| 833 | |
| 834 | static struct genl_family dp_flow_genl_family = { |
| 835 | .id = GENL_ID_GENERATE, |
| 836 | .hdrsize = sizeof(struct ovs_header), |
| 837 | .name = OVS_FLOW_FAMILY, |
| 838 | .version = OVS_FLOW_VERSION, |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 839 | .maxattr = OVS_FLOW_ATTR_MAX, |
Pravin B Shelar | 3a4e0d6 | 2013-04-23 07:48:48 +0000 | [diff] [blame^] | 840 | .netnsok = true, |
| 841 | .parallel_ops = true, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 842 | }; |
| 843 | |
| 844 | static struct genl_multicast_group ovs_dp_flow_multicast_group = { |
| 845 | .name = OVS_FLOW_MCGROUP |
| 846 | }; |
| 847 | |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 848 | static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts) |
| 849 | { |
| 850 | return NLMSG_ALIGN(sizeof(struct ovs_header)) |
| 851 | + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */ |
| 852 | + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ |
| 853 | + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ |
| 854 | + nla_total_size(8) /* OVS_FLOW_ATTR_USED */ |
| 855 | + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */ |
| 856 | } |
| 857 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 858 | /* Called with ovs_mutex. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 859 | static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 860 | struct sk_buff *skb, u32 portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 861 | u32 seq, u32 flags, u8 cmd) |
| 862 | { |
| 863 | const int skb_orig_len = skb->len; |
| 864 | const struct sw_flow_actions *sf_acts; |
| 865 | struct ovs_flow_stats stats; |
| 866 | struct ovs_header *ovs_header; |
| 867 | struct nlattr *nla; |
| 868 | unsigned long used; |
| 869 | u8 tcp_flags; |
| 870 | int err; |
| 871 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 872 | sf_acts = ovsl_dereference(flow->sf_acts); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 873 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 874 | ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 875 | if (!ovs_header) |
| 876 | return -EMSGSIZE; |
| 877 | |
| 878 | ovs_header->dp_ifindex = get_dpifindex(dp); |
| 879 | |
| 880 | nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY); |
| 881 | if (!nla) |
| 882 | goto nla_put_failure; |
| 883 | err = ovs_flow_to_nlattrs(&flow->key, skb); |
| 884 | if (err) |
| 885 | goto error; |
| 886 | nla_nest_end(skb, nla); |
| 887 | |
| 888 | spin_lock_bh(&flow->lock); |
| 889 | used = flow->used; |
| 890 | stats.n_packets = flow->packet_count; |
| 891 | stats.n_bytes = flow->byte_count; |
| 892 | tcp_flags = flow->tcp_flags; |
| 893 | spin_unlock_bh(&flow->lock); |
| 894 | |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 895 | if (used && |
| 896 | nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) |
| 897 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 898 | |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 899 | if (stats.n_packets && |
| 900 | nla_put(skb, OVS_FLOW_ATTR_STATS, |
| 901 | sizeof(struct ovs_flow_stats), &stats)) |
| 902 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 903 | |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 904 | if (tcp_flags && |
| 905 | nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags)) |
| 906 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 907 | |
| 908 | /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if |
| 909 | * this is the first flow to be dumped into 'skb'. This is unusual for |
| 910 | * Netlink but individual action lists can be longer than |
| 911 | * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. |
| 912 | * The userspace caller can always fetch the actions separately if it |
| 913 | * really wants them. (Most userspace callers in fact don't care.) |
| 914 | * |
| 915 | * This can only fail for dump operations because the skb is always |
| 916 | * properly sized for single flows. |
| 917 | */ |
| 918 | err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len, |
| 919 | sf_acts->actions); |
| 920 | if (err < 0 && skb_orig_len) |
| 921 | goto error; |
| 922 | |
| 923 | return genlmsg_end(skb, ovs_header); |
| 924 | |
| 925 | nla_put_failure: |
| 926 | err = -EMSGSIZE; |
| 927 | error: |
| 928 | genlmsg_cancel(skb, ovs_header); |
| 929 | return err; |
| 930 | } |
| 931 | |
| 932 | static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow) |
| 933 | { |
| 934 | const struct sw_flow_actions *sf_acts; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 935 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 936 | sf_acts = ovsl_dereference(flow->sf_acts); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 937 | |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 938 | return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, |
| 942 | struct datapath *dp, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 943 | u32 portid, u32 seq, u8 cmd) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 944 | { |
| 945 | struct sk_buff *skb; |
| 946 | int retval; |
| 947 | |
| 948 | skb = ovs_flow_cmd_alloc_info(flow); |
| 949 | if (!skb) |
| 950 | return ERR_PTR(-ENOMEM); |
| 951 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 952 | retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 953 | BUG_ON(retval < 0); |
| 954 | return skb; |
| 955 | } |
| 956 | |
| 957 | static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info) |
| 958 | { |
| 959 | struct nlattr **a = info->attrs; |
| 960 | struct ovs_header *ovs_header = info->userhdr; |
| 961 | struct sw_flow_key key; |
| 962 | struct sw_flow *flow; |
| 963 | struct sk_buff *reply; |
| 964 | struct datapath *dp; |
| 965 | struct flow_table *table; |
| 966 | int error; |
| 967 | int key_len; |
| 968 | |
| 969 | /* Extract key. */ |
| 970 | error = -EINVAL; |
| 971 | if (!a[OVS_FLOW_ATTR_KEY]) |
| 972 | goto error; |
| 973 | error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); |
| 974 | if (error) |
| 975 | goto error; |
| 976 | |
| 977 | /* Validate actions. */ |
| 978 | if (a[OVS_FLOW_ATTR_ACTIONS]) { |
| 979 | error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0); |
| 980 | if (error) |
| 981 | goto error; |
| 982 | } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) { |
| 983 | error = -EINVAL; |
| 984 | goto error; |
| 985 | } |
| 986 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 987 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 988 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 989 | error = -ENODEV; |
| 990 | if (!dp) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 991 | goto err_unlock_ovs; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 992 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 993 | table = ovsl_dereference(dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 994 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
| 995 | if (!flow) { |
| 996 | struct sw_flow_actions *acts; |
| 997 | |
| 998 | /* Bail out if we're not allowed to create a new flow. */ |
| 999 | error = -ENOENT; |
| 1000 | if (info->genlhdr->cmd == OVS_FLOW_CMD_SET) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1001 | goto err_unlock_ovs; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1002 | |
| 1003 | /* Expand table, if necessary, to make room. */ |
| 1004 | if (ovs_flow_tbl_need_to_expand(table)) { |
| 1005 | struct flow_table *new_table; |
| 1006 | |
| 1007 | new_table = ovs_flow_tbl_expand(table); |
| 1008 | if (!IS_ERR(new_table)) { |
| 1009 | rcu_assign_pointer(dp->table, new_table); |
| 1010 | ovs_flow_tbl_deferred_destroy(table); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1011 | table = ovsl_dereference(dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | /* Allocate flow. */ |
| 1016 | flow = ovs_flow_alloc(); |
| 1017 | if (IS_ERR(flow)) { |
| 1018 | error = PTR_ERR(flow); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1019 | goto err_unlock_ovs; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1020 | } |
| 1021 | flow->key = key; |
| 1022 | clear_stats(flow); |
| 1023 | |
| 1024 | /* Obtain actions. */ |
| 1025 | acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]); |
| 1026 | error = PTR_ERR(acts); |
| 1027 | if (IS_ERR(acts)) |
| 1028 | goto error_free_flow; |
| 1029 | rcu_assign_pointer(flow->sf_acts, acts); |
| 1030 | |
| 1031 | /* Put flow in bucket. */ |
| 1032 | flow->hash = ovs_flow_hash(&key, key_len); |
| 1033 | ovs_flow_tbl_insert(table, flow); |
| 1034 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1035 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1036 | info->snd_seq, |
| 1037 | OVS_FLOW_CMD_NEW); |
| 1038 | } else { |
| 1039 | /* We found a matching flow. */ |
| 1040 | struct sw_flow_actions *old_acts; |
| 1041 | struct nlattr *acts_attrs; |
| 1042 | |
| 1043 | /* Bail out if we're not allowed to modify an existing flow. |
| 1044 | * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL |
| 1045 | * because Generic Netlink treats the latter as a dump |
| 1046 | * request. We also accept NLM_F_EXCL in case that bug ever |
| 1047 | * gets fixed. |
| 1048 | */ |
| 1049 | error = -EEXIST; |
| 1050 | if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW && |
| 1051 | info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1052 | goto err_unlock_ovs; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1053 | |
| 1054 | /* Update actions. */ |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1055 | old_acts = ovsl_dereference(flow->sf_acts); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1056 | acts_attrs = a[OVS_FLOW_ATTR_ACTIONS]; |
| 1057 | if (acts_attrs && |
| 1058 | (old_acts->actions_len != nla_len(acts_attrs) || |
| 1059 | memcmp(old_acts->actions, nla_data(acts_attrs), |
| 1060 | old_acts->actions_len))) { |
| 1061 | struct sw_flow_actions *new_acts; |
| 1062 | |
| 1063 | new_acts = ovs_flow_actions_alloc(acts_attrs); |
| 1064 | error = PTR_ERR(new_acts); |
| 1065 | if (IS_ERR(new_acts)) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1066 | goto err_unlock_ovs; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1067 | |
| 1068 | rcu_assign_pointer(flow->sf_acts, new_acts); |
| 1069 | ovs_flow_deferred_free_acts(old_acts); |
| 1070 | } |
| 1071 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1072 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1073 | info->snd_seq, OVS_FLOW_CMD_NEW); |
| 1074 | |
| 1075 | /* Clear stats. */ |
| 1076 | if (a[OVS_FLOW_ATTR_CLEAR]) { |
| 1077 | spin_lock_bh(&flow->lock); |
| 1078 | clear_stats(flow); |
| 1079 | spin_unlock_bh(&flow->lock); |
| 1080 | } |
| 1081 | } |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1082 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1083 | |
| 1084 | if (!IS_ERR(reply)) |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 1085 | ovs_notify(reply, info, &ovs_dp_flow_multicast_group); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1086 | else |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1087 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1088 | ovs_dp_flow_multicast_group.id, PTR_ERR(reply)); |
| 1089 | return 0; |
| 1090 | |
| 1091 | error_free_flow: |
| 1092 | ovs_flow_free(flow); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1093 | err_unlock_ovs: |
| 1094 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1095 | error: |
| 1096 | return error; |
| 1097 | } |
| 1098 | |
| 1099 | static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) |
| 1100 | { |
| 1101 | struct nlattr **a = info->attrs; |
| 1102 | struct ovs_header *ovs_header = info->userhdr; |
| 1103 | struct sw_flow_key key; |
| 1104 | struct sk_buff *reply; |
| 1105 | struct sw_flow *flow; |
| 1106 | struct datapath *dp; |
| 1107 | struct flow_table *table; |
| 1108 | int err; |
| 1109 | int key_len; |
| 1110 | |
| 1111 | if (!a[OVS_FLOW_ATTR_KEY]) |
| 1112 | return -EINVAL; |
| 1113 | err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); |
| 1114 | if (err) |
| 1115 | return err; |
| 1116 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1117 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1118 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1119 | if (!dp) { |
| 1120 | err = -ENODEV; |
| 1121 | goto unlock; |
| 1122 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1123 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1124 | table = ovsl_dereference(dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1125 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1126 | if (!flow) { |
| 1127 | err = -ENOENT; |
| 1128 | goto unlock; |
| 1129 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1130 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1131 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1132 | info->snd_seq, OVS_FLOW_CMD_NEW); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1133 | if (IS_ERR(reply)) { |
| 1134 | err = PTR_ERR(reply); |
| 1135 | goto unlock; |
| 1136 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1137 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1138 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1139 | return genlmsg_reply(reply, info); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1140 | unlock: |
| 1141 | ovs_unlock(); |
| 1142 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) |
| 1146 | { |
| 1147 | struct nlattr **a = info->attrs; |
| 1148 | struct ovs_header *ovs_header = info->userhdr; |
| 1149 | struct sw_flow_key key; |
| 1150 | struct sk_buff *reply; |
| 1151 | struct sw_flow *flow; |
| 1152 | struct datapath *dp; |
| 1153 | struct flow_table *table; |
| 1154 | int err; |
| 1155 | int key_len; |
| 1156 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1157 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1158 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1159 | if (!dp) { |
| 1160 | err = -ENODEV; |
| 1161 | goto unlock; |
| 1162 | } |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1163 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1164 | if (!a[OVS_FLOW_ATTR_KEY]) { |
| 1165 | err = flush_flows(dp); |
| 1166 | goto unlock; |
| 1167 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1168 | err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); |
| 1169 | if (err) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1170 | goto unlock; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1171 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1172 | table = ovsl_dereference(dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1173 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1174 | if (!flow) { |
| 1175 | err = -ENOENT; |
| 1176 | goto unlock; |
| 1177 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1178 | |
| 1179 | reply = ovs_flow_cmd_alloc_info(flow); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1180 | if (!reply) { |
| 1181 | err = -ENOMEM; |
| 1182 | goto unlock; |
| 1183 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1184 | |
| 1185 | ovs_flow_tbl_remove(table, flow); |
| 1186 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1187 | err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1188 | info->snd_seq, 0, OVS_FLOW_CMD_DEL); |
| 1189 | BUG_ON(err < 0); |
| 1190 | |
| 1191 | ovs_flow_deferred_free(flow); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1192 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1193 | |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 1194 | ovs_notify(reply, info, &ovs_dp_flow_multicast_group); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1195 | return 0; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1196 | unlock: |
| 1197 | ovs_unlock(); |
| 1198 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1202 | { |
| 1203 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
| 1204 | struct datapath *dp; |
| 1205 | struct flow_table *table; |
| 1206 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1207 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1208 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1209 | if (!dp) { |
| 1210 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1211 | return -ENODEV; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1212 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1213 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1214 | table = ovsl_dereference(dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1215 | |
| 1216 | for (;;) { |
| 1217 | struct sw_flow *flow; |
| 1218 | u32 bucket, obj; |
| 1219 | |
| 1220 | bucket = cb->args[0]; |
| 1221 | obj = cb->args[1]; |
| 1222 | flow = ovs_flow_tbl_next(table, &bucket, &obj); |
| 1223 | if (!flow) |
| 1224 | break; |
| 1225 | |
| 1226 | if (ovs_flow_cmd_fill_info(flow, dp, skb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1227 | NETLINK_CB(cb->skb).portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1228 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1229 | OVS_FLOW_CMD_NEW) < 0) |
| 1230 | break; |
| 1231 | |
| 1232 | cb->args[0] = bucket; |
| 1233 | cb->args[1] = obj; |
| 1234 | } |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1235 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1236 | return skb->len; |
| 1237 | } |
| 1238 | |
| 1239 | static struct genl_ops dp_flow_genl_ops[] = { |
| 1240 | { .cmd = OVS_FLOW_CMD_NEW, |
| 1241 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1242 | .policy = flow_policy, |
| 1243 | .doit = ovs_flow_cmd_new_or_set |
| 1244 | }, |
| 1245 | { .cmd = OVS_FLOW_CMD_DEL, |
| 1246 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1247 | .policy = flow_policy, |
| 1248 | .doit = ovs_flow_cmd_del |
| 1249 | }, |
| 1250 | { .cmd = OVS_FLOW_CMD_GET, |
| 1251 | .flags = 0, /* OK for unprivileged users. */ |
| 1252 | .policy = flow_policy, |
| 1253 | .doit = ovs_flow_cmd_get, |
| 1254 | .dumpit = ovs_flow_cmd_dump |
| 1255 | }, |
| 1256 | { .cmd = OVS_FLOW_CMD_SET, |
| 1257 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1258 | .policy = flow_policy, |
| 1259 | .doit = ovs_flow_cmd_new_or_set, |
| 1260 | }, |
| 1261 | }; |
| 1262 | |
| 1263 | static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { |
| 1264 | [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, |
| 1265 | [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, |
| 1266 | }; |
| 1267 | |
| 1268 | static struct genl_family dp_datapath_genl_family = { |
| 1269 | .id = GENL_ID_GENERATE, |
| 1270 | .hdrsize = sizeof(struct ovs_header), |
| 1271 | .name = OVS_DATAPATH_FAMILY, |
| 1272 | .version = OVS_DATAPATH_VERSION, |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1273 | .maxattr = OVS_DP_ATTR_MAX, |
Pravin B Shelar | 3a4e0d6 | 2013-04-23 07:48:48 +0000 | [diff] [blame^] | 1274 | .netnsok = true, |
| 1275 | .parallel_ops = true, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1276 | }; |
| 1277 | |
| 1278 | static struct genl_multicast_group ovs_dp_datapath_multicast_group = { |
| 1279 | .name = OVS_DATAPATH_MCGROUP |
| 1280 | }; |
| 1281 | |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 1282 | static size_t ovs_dp_cmd_msg_size(void) |
| 1283 | { |
| 1284 | size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); |
| 1285 | |
| 1286 | msgsize += nla_total_size(IFNAMSIZ); |
| 1287 | msgsize += nla_total_size(sizeof(struct ovs_dp_stats)); |
| 1288 | |
| 1289 | return msgsize; |
| 1290 | } |
| 1291 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1292 | static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1293 | u32 portid, u32 seq, u32 flags, u8 cmd) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1294 | { |
| 1295 | struct ovs_header *ovs_header; |
| 1296 | struct ovs_dp_stats dp_stats; |
| 1297 | int err; |
| 1298 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1299 | ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1300 | flags, cmd); |
| 1301 | if (!ovs_header) |
| 1302 | goto error; |
| 1303 | |
| 1304 | ovs_header->dp_ifindex = get_dpifindex(dp); |
| 1305 | |
| 1306 | rcu_read_lock(); |
| 1307 | err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); |
| 1308 | rcu_read_unlock(); |
| 1309 | if (err) |
| 1310 | goto nla_put_failure; |
| 1311 | |
| 1312 | get_dp_stats(dp, &dp_stats); |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1313 | if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats)) |
| 1314 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1315 | |
| 1316 | return genlmsg_end(skb, ovs_header); |
| 1317 | |
| 1318 | nla_put_failure: |
| 1319 | genlmsg_cancel(skb, ovs_header); |
| 1320 | error: |
| 1321 | return -EMSGSIZE; |
| 1322 | } |
| 1323 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1324 | static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1325 | u32 seq, u8 cmd) |
| 1326 | { |
| 1327 | struct sk_buff *skb; |
| 1328 | int retval; |
| 1329 | |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 1330 | skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1331 | if (!skb) |
| 1332 | return ERR_PTR(-ENOMEM); |
| 1333 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1334 | retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1335 | if (retval < 0) { |
| 1336 | kfree_skb(skb); |
| 1337 | return ERR_PTR(retval); |
| 1338 | } |
| 1339 | return skb; |
| 1340 | } |
| 1341 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1342 | /* Called with ovs_mutex. */ |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1343 | static struct datapath *lookup_datapath(struct net *net, |
| 1344 | struct ovs_header *ovs_header, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1345 | struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
| 1346 | { |
| 1347 | struct datapath *dp; |
| 1348 | |
| 1349 | if (!a[OVS_DP_ATTR_NAME]) |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1350 | dp = get_dp(net, ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1351 | else { |
| 1352 | struct vport *vport; |
| 1353 | |
| 1354 | rcu_read_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1355 | vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1356 | dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; |
| 1357 | rcu_read_unlock(); |
| 1358 | } |
| 1359 | return dp ? dp : ERR_PTR(-ENODEV); |
| 1360 | } |
| 1361 | |
| 1362 | static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) |
| 1363 | { |
| 1364 | struct nlattr **a = info->attrs; |
| 1365 | struct vport_parms parms; |
| 1366 | struct sk_buff *reply; |
| 1367 | struct datapath *dp; |
| 1368 | struct vport *vport; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1369 | struct ovs_net *ovs_net; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1370 | int err, i; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1371 | |
| 1372 | err = -EINVAL; |
| 1373 | if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) |
| 1374 | goto err; |
| 1375 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1376 | ovs_lock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1377 | |
| 1378 | err = -ENOMEM; |
| 1379 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); |
| 1380 | if (dp == NULL) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1381 | goto err_unlock_ovs; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1382 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1383 | ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1384 | |
| 1385 | /* Allocate table. */ |
| 1386 | err = -ENOMEM; |
| 1387 | rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS)); |
| 1388 | if (!dp->table) |
| 1389 | goto err_free_dp; |
| 1390 | |
| 1391 | dp->stats_percpu = alloc_percpu(struct dp_stats_percpu); |
| 1392 | if (!dp->stats_percpu) { |
| 1393 | err = -ENOMEM; |
| 1394 | goto err_destroy_table; |
| 1395 | } |
| 1396 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1397 | dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), |
| 1398 | GFP_KERNEL); |
| 1399 | if (!dp->ports) { |
| 1400 | err = -ENOMEM; |
| 1401 | goto err_destroy_percpu; |
| 1402 | } |
| 1403 | |
| 1404 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) |
| 1405 | INIT_HLIST_HEAD(&dp->ports[i]); |
| 1406 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1407 | /* Set up our datapath device. */ |
| 1408 | parms.name = nla_data(a[OVS_DP_ATTR_NAME]); |
| 1409 | parms.type = OVS_VPORT_TYPE_INTERNAL; |
| 1410 | parms.options = NULL; |
| 1411 | parms.dp = dp; |
| 1412 | parms.port_no = OVSP_LOCAL; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1413 | parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1414 | |
| 1415 | vport = new_vport(&parms); |
| 1416 | if (IS_ERR(vport)) { |
| 1417 | err = PTR_ERR(vport); |
| 1418 | if (err == -EBUSY) |
| 1419 | err = -EEXIST; |
| 1420 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1421 | goto err_destroy_ports_array; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1424 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1425 | info->snd_seq, OVS_DP_CMD_NEW); |
| 1426 | err = PTR_ERR(reply); |
| 1427 | if (IS_ERR(reply)) |
| 1428 | goto err_destroy_local_port; |
| 1429 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1430 | ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); |
| 1431 | list_add_tail(&dp->list_node, &ovs_net->dps); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1432 | |
| 1433 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1434 | |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 1435 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1436 | return 0; |
| 1437 | |
| 1438 | err_destroy_local_port: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1439 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1440 | err_destroy_ports_array: |
| 1441 | kfree(dp->ports); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1442 | err_destroy_percpu: |
| 1443 | free_percpu(dp->stats_percpu); |
| 1444 | err_destroy_table: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1445 | ovs_flow_tbl_destroy(ovsl_dereference(dp->table)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1446 | err_free_dp: |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1447 | release_net(ovs_dp_get_net(dp)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1448 | kfree(dp); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1449 | err_unlock_ovs: |
| 1450 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1451 | err: |
| 1452 | return err; |
| 1453 | } |
| 1454 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1455 | /* Called with ovs_mutex. */ |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1456 | static void __dp_destroy(struct datapath *dp) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1457 | { |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1458 | int i; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1459 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1460 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
| 1461 | struct vport *vport; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1462 | struct hlist_node *n; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1463 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1464 | hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1465 | if (vport->port_no != OVSP_LOCAL) |
| 1466 | ovs_dp_detach_port(vport); |
| 1467 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1468 | |
| 1469 | list_del(&dp->list_node); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1470 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1471 | /* OVSP_LOCAL is datapath internal port. We need to make sure that |
| 1472 | * all port in datapath are destroyed first before freeing datapath. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1473 | */ |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1474 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1475 | |
| 1476 | call_rcu(&dp->rcu, destroy_dp_rcu); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) |
| 1480 | { |
| 1481 | struct sk_buff *reply; |
| 1482 | struct datapath *dp; |
| 1483 | int err; |
| 1484 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1485 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1486 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
| 1487 | err = PTR_ERR(dp); |
| 1488 | if (IS_ERR(dp)) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1489 | goto unlock; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1490 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1491 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1492 | info->snd_seq, OVS_DP_CMD_DEL); |
| 1493 | err = PTR_ERR(reply); |
| 1494 | if (IS_ERR(reply)) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1495 | goto unlock; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1496 | |
| 1497 | __dp_destroy(dp); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1498 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1499 | |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 1500 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1501 | |
| 1502 | return 0; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1503 | unlock: |
| 1504 | ovs_unlock(); |
| 1505 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) |
| 1509 | { |
| 1510 | struct sk_buff *reply; |
| 1511 | struct datapath *dp; |
| 1512 | int err; |
| 1513 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1514 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1515 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1516 | err = PTR_ERR(dp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1517 | if (IS_ERR(dp)) |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1518 | goto unlock; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1519 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1520 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1521 | info->snd_seq, OVS_DP_CMD_NEW); |
| 1522 | if (IS_ERR(reply)) { |
| 1523 | err = PTR_ERR(reply); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1524 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1525 | ovs_dp_datapath_multicast_group.id, err); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1526 | err = 0; |
| 1527 | goto unlock; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1528 | } |
| 1529 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1530 | ovs_unlock(); |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 1531 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1532 | |
| 1533 | return 0; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1534 | unlock: |
| 1535 | ovs_unlock(); |
| 1536 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) |
| 1540 | { |
| 1541 | struct sk_buff *reply; |
| 1542 | struct datapath *dp; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1543 | int err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1544 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1545 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1546 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1547 | if (IS_ERR(dp)) { |
| 1548 | err = PTR_ERR(dp); |
| 1549 | goto unlock; |
| 1550 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1551 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1552 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1553 | info->snd_seq, OVS_DP_CMD_NEW); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1554 | if (IS_ERR(reply)) { |
| 1555 | err = PTR_ERR(reply); |
| 1556 | goto unlock; |
| 1557 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1558 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1559 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1560 | return genlmsg_reply(reply, info); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1561 | |
| 1562 | unlock: |
| 1563 | ovs_unlock(); |
| 1564 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1568 | { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1569 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1570 | struct datapath *dp; |
| 1571 | int skip = cb->args[0]; |
| 1572 | int i = 0; |
| 1573 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1574 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1575 | list_for_each_entry(dp, &ovs_net->dps, list_node) { |
Ben Pfaff | 77676fd | 2012-01-17 13:33:39 +0000 | [diff] [blame] | 1576 | if (i >= skip && |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1577 | ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1578 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1579 | OVS_DP_CMD_NEW) < 0) |
| 1580 | break; |
| 1581 | i++; |
| 1582 | } |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1583 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1584 | |
| 1585 | cb->args[0] = i; |
| 1586 | |
| 1587 | return skb->len; |
| 1588 | } |
| 1589 | |
| 1590 | static struct genl_ops dp_datapath_genl_ops[] = { |
| 1591 | { .cmd = OVS_DP_CMD_NEW, |
| 1592 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1593 | .policy = datapath_policy, |
| 1594 | .doit = ovs_dp_cmd_new |
| 1595 | }, |
| 1596 | { .cmd = OVS_DP_CMD_DEL, |
| 1597 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1598 | .policy = datapath_policy, |
| 1599 | .doit = ovs_dp_cmd_del |
| 1600 | }, |
| 1601 | { .cmd = OVS_DP_CMD_GET, |
| 1602 | .flags = 0, /* OK for unprivileged users. */ |
| 1603 | .policy = datapath_policy, |
| 1604 | .doit = ovs_dp_cmd_get, |
| 1605 | .dumpit = ovs_dp_cmd_dump |
| 1606 | }, |
| 1607 | { .cmd = OVS_DP_CMD_SET, |
| 1608 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1609 | .policy = datapath_policy, |
| 1610 | .doit = ovs_dp_cmd_set, |
| 1611 | }, |
| 1612 | }; |
| 1613 | |
| 1614 | static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { |
| 1615 | [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, |
| 1616 | [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, |
| 1617 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, |
| 1618 | [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, |
| 1619 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, |
| 1620 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, |
| 1621 | }; |
| 1622 | |
| 1623 | static struct genl_family dp_vport_genl_family = { |
| 1624 | .id = GENL_ID_GENERATE, |
| 1625 | .hdrsize = sizeof(struct ovs_header), |
| 1626 | .name = OVS_VPORT_FAMILY, |
| 1627 | .version = OVS_VPORT_VERSION, |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1628 | .maxattr = OVS_VPORT_ATTR_MAX, |
Pravin B Shelar | 3a4e0d6 | 2013-04-23 07:48:48 +0000 | [diff] [blame^] | 1629 | .netnsok = true, |
| 1630 | .parallel_ops = true, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1631 | }; |
| 1632 | |
| 1633 | struct genl_multicast_group ovs_dp_vport_multicast_group = { |
| 1634 | .name = OVS_VPORT_MCGROUP |
| 1635 | }; |
| 1636 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1637 | /* Called with ovs_mutex or RCU read lock. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1638 | static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1639 | u32 portid, u32 seq, u32 flags, u8 cmd) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1640 | { |
| 1641 | struct ovs_header *ovs_header; |
| 1642 | struct ovs_vport_stats vport_stats; |
| 1643 | int err; |
| 1644 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1645 | ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1646 | flags, cmd); |
| 1647 | if (!ovs_header) |
| 1648 | return -EMSGSIZE; |
| 1649 | |
| 1650 | ovs_header->dp_ifindex = get_dpifindex(vport->dp); |
| 1651 | |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1652 | if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || |
| 1653 | nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || |
| 1654 | nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) || |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1655 | nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid)) |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1656 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1657 | |
| 1658 | ovs_vport_get_stats(vport, &vport_stats); |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1659 | if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), |
| 1660 | &vport_stats)) |
| 1661 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1662 | |
| 1663 | err = ovs_vport_get_options(vport, skb); |
| 1664 | if (err == -EMSGSIZE) |
| 1665 | goto error; |
| 1666 | |
| 1667 | return genlmsg_end(skb, ovs_header); |
| 1668 | |
| 1669 | nla_put_failure: |
| 1670 | err = -EMSGSIZE; |
| 1671 | error: |
| 1672 | genlmsg_cancel(skb, ovs_header); |
| 1673 | return err; |
| 1674 | } |
| 1675 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1676 | /* Called with ovs_mutex or RCU read lock. */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1677 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1678 | u32 seq, u8 cmd) |
| 1679 | { |
| 1680 | struct sk_buff *skb; |
| 1681 | int retval; |
| 1682 | |
| 1683 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 1684 | if (!skb) |
| 1685 | return ERR_PTR(-ENOMEM); |
| 1686 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1687 | retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 1688 | BUG_ON(retval < 0); |
| 1689 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1690 | return skb; |
| 1691 | } |
| 1692 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1693 | /* Called with ovs_mutex or RCU read lock. */ |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1694 | static struct vport *lookup_vport(struct net *net, |
| 1695 | struct ovs_header *ovs_header, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1696 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
| 1697 | { |
| 1698 | struct datapath *dp; |
| 1699 | struct vport *vport; |
| 1700 | |
| 1701 | if (a[OVS_VPORT_ATTR_NAME]) { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1702 | vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1703 | if (!vport) |
| 1704 | return ERR_PTR(-ENODEV); |
Ben Pfaff | 651a68e | 2012-03-06 15:04:04 -0800 | [diff] [blame] | 1705 | if (ovs_header->dp_ifindex && |
| 1706 | ovs_header->dp_ifindex != get_dpifindex(vport->dp)) |
| 1707 | return ERR_PTR(-ENODEV); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1708 | return vport; |
| 1709 | } else if (a[OVS_VPORT_ATTR_PORT_NO]) { |
| 1710 | u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); |
| 1711 | |
| 1712 | if (port_no >= DP_MAX_PORTS) |
| 1713 | return ERR_PTR(-EFBIG); |
| 1714 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1715 | dp = get_dp(net, ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1716 | if (!dp) |
| 1717 | return ERR_PTR(-ENODEV); |
| 1718 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1719 | vport = ovs_vport_ovsl_rcu(dp, port_no); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1720 | if (!vport) |
Jarno Rajahalme | 14408db | 2013-01-09 14:27:35 -0800 | [diff] [blame] | 1721 | return ERR_PTR(-ENODEV); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1722 | return vport; |
| 1723 | } else |
| 1724 | return ERR_PTR(-EINVAL); |
| 1725 | } |
| 1726 | |
| 1727 | static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) |
| 1728 | { |
| 1729 | struct nlattr **a = info->attrs; |
| 1730 | struct ovs_header *ovs_header = info->userhdr; |
| 1731 | struct vport_parms parms; |
| 1732 | struct sk_buff *reply; |
| 1733 | struct vport *vport; |
| 1734 | struct datapath *dp; |
| 1735 | u32 port_no; |
| 1736 | int err; |
| 1737 | |
| 1738 | err = -EINVAL; |
| 1739 | if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || |
| 1740 | !a[OVS_VPORT_ATTR_UPCALL_PID]) |
| 1741 | goto exit; |
| 1742 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1743 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1744 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1745 | err = -ENODEV; |
| 1746 | if (!dp) |
| 1747 | goto exit_unlock; |
| 1748 | |
| 1749 | if (a[OVS_VPORT_ATTR_PORT_NO]) { |
| 1750 | port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); |
| 1751 | |
| 1752 | err = -EFBIG; |
| 1753 | if (port_no >= DP_MAX_PORTS) |
| 1754 | goto exit_unlock; |
| 1755 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1756 | vport = ovs_vport_ovsl(dp, port_no); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1757 | err = -EBUSY; |
| 1758 | if (vport) |
| 1759 | goto exit_unlock; |
| 1760 | } else { |
| 1761 | for (port_no = 1; ; port_no++) { |
| 1762 | if (port_no >= DP_MAX_PORTS) { |
| 1763 | err = -EFBIG; |
| 1764 | goto exit_unlock; |
| 1765 | } |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1766 | vport = ovs_vport_ovsl(dp, port_no); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1767 | if (!vport) |
| 1768 | break; |
| 1769 | } |
| 1770 | } |
| 1771 | |
| 1772 | parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); |
| 1773 | parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); |
| 1774 | parms.options = a[OVS_VPORT_ATTR_OPTIONS]; |
| 1775 | parms.dp = dp; |
| 1776 | parms.port_no = port_no; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1777 | parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1778 | |
| 1779 | vport = new_vport(&parms); |
| 1780 | err = PTR_ERR(vport); |
| 1781 | if (IS_ERR(vport)) |
| 1782 | goto exit_unlock; |
| 1783 | |
Rich Lane | cb7c5bd | 2013-02-08 13:18:01 -0800 | [diff] [blame] | 1784 | err = 0; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1785 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1786 | OVS_VPORT_CMD_NEW); |
| 1787 | if (IS_ERR(reply)) { |
| 1788 | err = PTR_ERR(reply); |
| 1789 | ovs_dp_detach_port(vport); |
| 1790 | goto exit_unlock; |
| 1791 | } |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 1792 | |
| 1793 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1794 | |
| 1795 | exit_unlock: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1796 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1797 | exit: |
| 1798 | return err; |
| 1799 | } |
| 1800 | |
| 1801 | static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) |
| 1802 | { |
| 1803 | struct nlattr **a = info->attrs; |
| 1804 | struct sk_buff *reply; |
| 1805 | struct vport *vport; |
| 1806 | int err; |
| 1807 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1808 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1809 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1810 | err = PTR_ERR(vport); |
| 1811 | if (IS_ERR(vport)) |
| 1812 | goto exit_unlock; |
| 1813 | |
| 1814 | err = 0; |
| 1815 | if (a[OVS_VPORT_ATTR_TYPE] && |
| 1816 | nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) |
| 1817 | err = -EINVAL; |
| 1818 | |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 1819 | reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1820 | if (!reply) { |
| 1821 | err = -ENOMEM; |
| 1822 | goto exit_unlock; |
| 1823 | } |
| 1824 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1825 | if (!err && a[OVS_VPORT_ATTR_OPTIONS]) |
| 1826 | err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); |
Ansis Atteka | 03fbf8b | 2012-04-09 12:12:12 -0700 | [diff] [blame] | 1827 | if (err) |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 1828 | goto exit_free; |
| 1829 | |
Ansis Atteka | 03fbf8b | 2012-04-09 12:12:12 -0700 | [diff] [blame] | 1830 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1831 | vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1832 | |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 1833 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
| 1834 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); |
| 1835 | BUG_ON(err < 0); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1836 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1837 | ovs_unlock(); |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 1838 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1839 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1840 | |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 1841 | rtnl_unlock(); |
| 1842 | return 0; |
| 1843 | |
| 1844 | exit_free: |
| 1845 | kfree_skb(reply); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1846 | exit_unlock: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1847 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1848 | return err; |
| 1849 | } |
| 1850 | |
| 1851 | static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) |
| 1852 | { |
| 1853 | struct nlattr **a = info->attrs; |
| 1854 | struct sk_buff *reply; |
| 1855 | struct vport *vport; |
| 1856 | int err; |
| 1857 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1858 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1859 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1860 | err = PTR_ERR(vport); |
| 1861 | if (IS_ERR(vport)) |
| 1862 | goto exit_unlock; |
| 1863 | |
| 1864 | if (vport->port_no == OVSP_LOCAL) { |
| 1865 | err = -EINVAL; |
| 1866 | goto exit_unlock; |
| 1867 | } |
| 1868 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1869 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1870 | OVS_VPORT_CMD_DEL); |
| 1871 | err = PTR_ERR(reply); |
| 1872 | if (IS_ERR(reply)) |
| 1873 | goto exit_unlock; |
| 1874 | |
Rich Lane | 734907e | 2013-02-08 09:30:23 -0800 | [diff] [blame] | 1875 | err = 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1876 | ovs_dp_detach_port(vport); |
| 1877 | |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 1878 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1879 | |
| 1880 | exit_unlock: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1881 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1882 | return err; |
| 1883 | } |
| 1884 | |
| 1885 | static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) |
| 1886 | { |
| 1887 | struct nlattr **a = info->attrs; |
| 1888 | struct ovs_header *ovs_header = info->userhdr; |
| 1889 | struct sk_buff *reply; |
| 1890 | struct vport *vport; |
| 1891 | int err; |
| 1892 | |
| 1893 | rcu_read_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1894 | vport = lookup_vport(sock_net(skb->sk), ovs_header, a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1895 | err = PTR_ERR(vport); |
| 1896 | if (IS_ERR(vport)) |
| 1897 | goto exit_unlock; |
| 1898 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1899 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1900 | OVS_VPORT_CMD_NEW); |
| 1901 | err = PTR_ERR(reply); |
| 1902 | if (IS_ERR(reply)) |
| 1903 | goto exit_unlock; |
| 1904 | |
| 1905 | rcu_read_unlock(); |
| 1906 | |
| 1907 | return genlmsg_reply(reply, info); |
| 1908 | |
| 1909 | exit_unlock: |
| 1910 | rcu_read_unlock(); |
| 1911 | return err; |
| 1912 | } |
| 1913 | |
| 1914 | static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1915 | { |
| 1916 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
| 1917 | struct datapath *dp; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1918 | int bucket = cb->args[0], skip = cb->args[1]; |
| 1919 | int i, j = 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1920 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1921 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1922 | if (!dp) |
| 1923 | return -ENODEV; |
| 1924 | |
| 1925 | rcu_read_lock(); |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1926 | for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1927 | struct vport *vport; |
| 1928 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1929 | j = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1930 | hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1931 | if (j >= skip && |
| 1932 | ovs_vport_cmd_fill_info(vport, skb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1933 | NETLINK_CB(cb->skb).portid, |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1934 | cb->nlh->nlmsg_seq, |
| 1935 | NLM_F_MULTI, |
| 1936 | OVS_VPORT_CMD_NEW) < 0) |
| 1937 | goto out; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1938 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1939 | j++; |
| 1940 | } |
| 1941 | skip = 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1942 | } |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1943 | out: |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1944 | rcu_read_unlock(); |
| 1945 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1946 | cb->args[0] = i; |
| 1947 | cb->args[1] = j; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1948 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1949 | return skb->len; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1952 | static struct genl_ops dp_vport_genl_ops[] = { |
| 1953 | { .cmd = OVS_VPORT_CMD_NEW, |
| 1954 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1955 | .policy = vport_policy, |
| 1956 | .doit = ovs_vport_cmd_new |
| 1957 | }, |
| 1958 | { .cmd = OVS_VPORT_CMD_DEL, |
| 1959 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1960 | .policy = vport_policy, |
| 1961 | .doit = ovs_vport_cmd_del |
| 1962 | }, |
| 1963 | { .cmd = OVS_VPORT_CMD_GET, |
| 1964 | .flags = 0, /* OK for unprivileged users. */ |
| 1965 | .policy = vport_policy, |
| 1966 | .doit = ovs_vport_cmd_get, |
| 1967 | .dumpit = ovs_vport_cmd_dump |
| 1968 | }, |
| 1969 | { .cmd = OVS_VPORT_CMD_SET, |
| 1970 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
| 1971 | .policy = vport_policy, |
| 1972 | .doit = ovs_vport_cmd_set, |
| 1973 | }, |
| 1974 | }; |
| 1975 | |
| 1976 | struct genl_family_and_ops { |
| 1977 | struct genl_family *family; |
| 1978 | struct genl_ops *ops; |
| 1979 | int n_ops; |
| 1980 | struct genl_multicast_group *group; |
| 1981 | }; |
| 1982 | |
| 1983 | static const struct genl_family_and_ops dp_genl_families[] = { |
| 1984 | { &dp_datapath_genl_family, |
| 1985 | dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops), |
| 1986 | &ovs_dp_datapath_multicast_group }, |
| 1987 | { &dp_vport_genl_family, |
| 1988 | dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops), |
| 1989 | &ovs_dp_vport_multicast_group }, |
| 1990 | { &dp_flow_genl_family, |
| 1991 | dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops), |
| 1992 | &ovs_dp_flow_multicast_group }, |
| 1993 | { &dp_packet_genl_family, |
| 1994 | dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops), |
| 1995 | NULL }, |
| 1996 | }; |
| 1997 | |
| 1998 | static void dp_unregister_genl(int n_families) |
| 1999 | { |
| 2000 | int i; |
| 2001 | |
| 2002 | for (i = 0; i < n_families; i++) |
| 2003 | genl_unregister_family(dp_genl_families[i].family); |
| 2004 | } |
| 2005 | |
| 2006 | static int dp_register_genl(void) |
| 2007 | { |
| 2008 | int n_registered; |
| 2009 | int err; |
| 2010 | int i; |
| 2011 | |
| 2012 | n_registered = 0; |
| 2013 | for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { |
| 2014 | const struct genl_family_and_ops *f = &dp_genl_families[i]; |
| 2015 | |
| 2016 | err = genl_register_family_with_ops(f->family, f->ops, |
| 2017 | f->n_ops); |
| 2018 | if (err) |
| 2019 | goto error; |
| 2020 | n_registered++; |
| 2021 | |
| 2022 | if (f->group) { |
| 2023 | err = genl_register_mc_group(f->family, f->group); |
| 2024 | if (err) |
| 2025 | goto error; |
| 2026 | } |
| 2027 | } |
| 2028 | |
| 2029 | return 0; |
| 2030 | |
| 2031 | error: |
| 2032 | dp_unregister_genl(n_registered); |
| 2033 | return err; |
| 2034 | } |
| 2035 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2036 | static void rehash_flow_table(struct work_struct *work) |
| 2037 | { |
| 2038 | struct datapath *dp; |
| 2039 | struct net *net; |
| 2040 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2041 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2042 | rtnl_lock(); |
| 2043 | for_each_net(net) { |
| 2044 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
| 2045 | |
| 2046 | list_for_each_entry(dp, &ovs_net->dps, list_node) { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2047 | struct flow_table *old_table = ovsl_dereference(dp->table); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2048 | struct flow_table *new_table; |
| 2049 | |
| 2050 | new_table = ovs_flow_tbl_rehash(old_table); |
| 2051 | if (!IS_ERR(new_table)) { |
| 2052 | rcu_assign_pointer(dp->table, new_table); |
| 2053 | ovs_flow_tbl_deferred_destroy(old_table); |
| 2054 | } |
| 2055 | } |
| 2056 | } |
| 2057 | rtnl_unlock(); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2058 | ovs_unlock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2059 | schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL); |
| 2060 | } |
| 2061 | |
| 2062 | static int __net_init ovs_init_net(struct net *net) |
| 2063 | { |
| 2064 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
| 2065 | |
| 2066 | INIT_LIST_HEAD(&ovs_net->dps); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2067 | INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2068 | return 0; |
| 2069 | } |
| 2070 | |
| 2071 | static void __net_exit ovs_exit_net(struct net *net) |
| 2072 | { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2073 | struct datapath *dp, *dp_next; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2074 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2075 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2076 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2077 | list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) |
| 2078 | __dp_destroy(dp); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2079 | ovs_unlock(); |
| 2080 | |
| 2081 | cancel_work_sync(&ovs_net->dp_notify_work); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | static struct pernet_operations ovs_net_ops = { |
| 2085 | .init = ovs_init_net, |
| 2086 | .exit = ovs_exit_net, |
| 2087 | .id = &ovs_net_id, |
| 2088 | .size = sizeof(struct ovs_net), |
| 2089 | }; |
| 2090 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2091 | static int __init dp_init(void) |
| 2092 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2093 | int err; |
| 2094 | |
YOSHIFUJI Hideaki / 吉藤英明 | 3523b29 | 2013-01-09 07:19:55 +0000 | [diff] [blame] | 2095 | BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2096 | |
| 2097 | pr_info("Open vSwitch switching datapath\n"); |
| 2098 | |
| 2099 | err = ovs_flow_init(); |
| 2100 | if (err) |
| 2101 | goto error; |
| 2102 | |
| 2103 | err = ovs_vport_init(); |
| 2104 | if (err) |
| 2105 | goto error_flow_exit; |
| 2106 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2107 | err = register_pernet_device(&ovs_net_ops); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2108 | if (err) |
| 2109 | goto error_vport_exit; |
| 2110 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2111 | err = register_netdevice_notifier(&ovs_dp_device_notifier); |
| 2112 | if (err) |
| 2113 | goto error_netns_exit; |
| 2114 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2115 | err = dp_register_genl(); |
| 2116 | if (err < 0) |
| 2117 | goto error_unreg_notifier; |
| 2118 | |
| 2119 | schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL); |
| 2120 | |
| 2121 | return 0; |
| 2122 | |
| 2123 | error_unreg_notifier: |
| 2124 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2125 | error_netns_exit: |
| 2126 | unregister_pernet_device(&ovs_net_ops); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2127 | error_vport_exit: |
| 2128 | ovs_vport_exit(); |
| 2129 | error_flow_exit: |
| 2130 | ovs_flow_exit(); |
| 2131 | error: |
| 2132 | return err; |
| 2133 | } |
| 2134 | |
| 2135 | static void dp_cleanup(void) |
| 2136 | { |
| 2137 | cancel_delayed_work_sync(&rehash_flow_wq); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2138 | dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); |
| 2139 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2140 | unregister_pernet_device(&ovs_net_ops); |
| 2141 | rcu_barrier(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2142 | ovs_vport_exit(); |
| 2143 | ovs_flow_exit(); |
| 2144 | } |
| 2145 | |
| 2146 | module_init(dp_init); |
| 2147 | module_exit(dp_cleanup); |
| 2148 | |
| 2149 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); |
| 2150 | MODULE_LICENSE("GPL"); |