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