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