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