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