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