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