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