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