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