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