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