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