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