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