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