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