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