Thomas Gleixner | c942299 | 2019-05-29 07:12:43 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2 | /* |
Ben Pfaff | ad55200 | 2014-05-06 16:48:38 -0700 | [diff] [blame] | 3 | * Copyright (c) 2007-2014 Nicira, Inc. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/if_arp.h> |
| 11 | #include <linux/if_vlan.h> |
| 12 | #include <linux/in.h> |
| 13 | #include <linux/ip.h> |
| 14 | #include <linux/jhash.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/time.h> |
| 17 | #include <linux/etherdevice.h> |
| 18 | #include <linux/genetlink.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/kthread.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/percpu.h> |
| 23 | #include <linux/rcupdate.h> |
| 24 | #include <linux/tcp.h> |
| 25 | #include <linux/udp.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 26 | #include <linux/ethtool.h> |
| 27 | #include <linux/wait.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 28 | #include <asm/div64.h> |
| 29 | #include <linux/highmem.h> |
| 30 | #include <linux/netfilter_bridge.h> |
| 31 | #include <linux/netfilter_ipv4.h> |
| 32 | #include <linux/inetdevice.h> |
| 33 | #include <linux/list.h> |
| 34 | #include <linux/openvswitch.h> |
| 35 | #include <linux/rculist.h> |
| 36 | #include <linux/dmi.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 37 | #include <net/genetlink.h> |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 38 | #include <net/net_namespace.h> |
| 39 | #include <net/netns/generic.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 40 | |
| 41 | #include "datapath.h" |
| 42 | #include "flow.h" |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 43 | #include "flow_table.h" |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 44 | #include "flow_netlink.h" |
Andy Zhou | 96fbc13 | 2017-11-10 12:09:42 -0800 | [diff] [blame] | 45 | #include "meter.h" |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 46 | #include "vport-internal_dev.h" |
Thomas Graf | cff63a5 | 2013-04-29 13:06:41 +0000 | [diff] [blame] | 47 | #include "vport-netdev.h" |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 48 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 49 | unsigned int ovs_net_id __read_mostly; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 50 | |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 51 | static struct genl_family dp_packet_genl_family; |
| 52 | static struct genl_family dp_flow_genl_family; |
| 53 | static struct genl_family dp_datapath_genl_family; |
| 54 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 55 | static const struct nla_policy flow_policy[]; |
| 56 | |
stephen hemminger | 48e48a7 | 2014-07-16 11:25:52 -0700 | [diff] [blame] | 57 | static const struct genl_multicast_group ovs_dp_flow_multicast_group = { |
| 58 | .name = OVS_FLOW_MCGROUP, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
stephen hemminger | 48e48a7 | 2014-07-16 11:25:52 -0700 | [diff] [blame] | 61 | static const struct genl_multicast_group ovs_dp_datapath_multicast_group = { |
| 62 | .name = OVS_DATAPATH_MCGROUP, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
stephen hemminger | 48e48a7 | 2014-07-16 11:25:52 -0700 | [diff] [blame] | 65 | static const struct genl_multicast_group ovs_dp_vport_multicast_group = { |
| 66 | .name = OVS_VPORT_MCGROUP, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 69 | /* Check if need to build a reply message. |
| 70 | * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */ |
Samuel Gauthier | 9b67aa4 | 2014-09-18 10:31:04 +0200 | [diff] [blame] | 71 | static bool ovs_must_notify(struct genl_family *family, struct genl_info *info, |
| 72 | unsigned int group) |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 73 | { |
| 74 | return info->nlhdr->nlmsg_flags & NLM_F_ECHO || |
Johannes Berg | f8403a2 | 2014-12-22 18:56:36 +0100 | [diff] [blame] | 75 | genl_has_listeners(family, genl_info_net(info), group); |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 78 | static void ovs_notify(struct genl_family *family, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 79 | struct sk_buff *skb, struct genl_info *info) |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 80 | { |
Jiri Benc | 92c14d9 | 2015-09-22 18:56:43 +0200 | [diff] [blame] | 81 | genl_notify(family, skb, info, 0, GFP_KERNEL); |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 84 | /** |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 85 | * DOC: Locking: |
| 86 | * |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 87 | * All writes e.g. Writes to device state (add/remove datapath, port, set |
| 88 | * operations on vports, etc.), Writes to other state (flow table |
| 89 | * modifications, set miscellaneous datapath parameters, etc.) are protected |
| 90 | * by ovs_lock. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 91 | * |
| 92 | * Reads are protected by RCU. |
| 93 | * |
| 94 | * There are a few special cases (mostly stats) that have their own |
| 95 | * synchronization but they nest under all of above and don't interact with |
| 96 | * each other. |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 97 | * |
| 98 | * The RTNL lock nests inside ovs_mutex. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 99 | */ |
| 100 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 101 | static DEFINE_MUTEX(ovs_mutex); |
| 102 | |
| 103 | void ovs_lock(void) |
| 104 | { |
| 105 | mutex_lock(&ovs_mutex); |
| 106 | } |
| 107 | |
| 108 | void ovs_unlock(void) |
| 109 | { |
| 110 | mutex_unlock(&ovs_mutex); |
| 111 | } |
| 112 | |
| 113 | #ifdef CONFIG_LOCKDEP |
| 114 | int lockdep_ovsl_is_held(void) |
| 115 | { |
| 116 | if (debug_locks) |
| 117 | return lockdep_is_held(&ovs_mutex); |
| 118 | else |
| 119 | return 1; |
| 120 | } |
| 121 | #endif |
| 122 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 123 | static struct vport *new_vport(const struct vport_parms *); |
Thomas Graf | 8055a89 | 2013-12-13 15:22:20 +0100 | [diff] [blame] | 124 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *, |
Pravin B Shelar | e8eedb8 | 2014-11-06 06:57:27 -0800 | [diff] [blame] | 125 | const struct sw_flow_key *, |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 126 | const struct dp_upcall_info *, |
| 127 | uint32_t cutlen); |
Thomas Graf | 8055a89 | 2013-12-13 15:22:20 +0100 | [diff] [blame] | 128 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *, |
Pravin B Shelar | e8eedb8 | 2014-11-06 06:57:27 -0800 | [diff] [blame] | 129 | const struct sw_flow_key *, |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 130 | const struct dp_upcall_info *, |
| 131 | uint32_t cutlen); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 132 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 133 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 134 | const char *ovs_dp_name(const struct datapath *dp) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 135 | { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 136 | struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); |
Thomas Graf | c9db965 | 2015-07-21 10:44:05 +0200 | [diff] [blame] | 137 | return ovs_vport_name(vport); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 140 | static int get_dpifindex(const struct datapath *dp) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 141 | { |
| 142 | struct vport *local; |
| 143 | int ifindex; |
| 144 | |
| 145 | rcu_read_lock(); |
| 146 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 147 | local = ovs_vport_rcu(dp, OVSP_LOCAL); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 148 | if (local) |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 149 | ifindex = local->dev->ifindex; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 150 | else |
| 151 | ifindex = 0; |
| 152 | |
| 153 | rcu_read_unlock(); |
| 154 | |
| 155 | return ifindex; |
| 156 | } |
| 157 | |
| 158 | static void destroy_dp_rcu(struct rcu_head *rcu) |
| 159 | { |
| 160 | struct datapath *dp = container_of(rcu, struct datapath, rcu); |
| 161 | |
Pravin B Shelar | 9b996e5 | 2014-05-06 18:41:20 -0700 | [diff] [blame] | 162 | ovs_flow_tbl_destroy(&dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 163 | free_percpu(dp->stats_percpu); |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 164 | kfree(dp->ports); |
Andy Zhou | 96fbc13 | 2017-11-10 12:09:42 -0800 | [diff] [blame] | 165 | ovs_meters_exit(dp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 166 | kfree(dp); |
| 167 | } |
| 168 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 169 | static struct hlist_head *vport_hash_bucket(const struct datapath *dp, |
| 170 | u16 port_no) |
| 171 | { |
| 172 | return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; |
| 173 | } |
| 174 | |
Jarno Rajahalme | bb6f9a7 | 2014-05-05 11:32:17 -0700 | [diff] [blame] | 175 | /* Called with ovs_mutex or RCU read lock. */ |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 176 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) |
| 177 | { |
| 178 | struct vport *vport; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 179 | struct hlist_head *head; |
| 180 | |
| 181 | head = vport_hash_bucket(dp, port_no); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 182 | hlist_for_each_entry_rcu(vport, head, dp_hash_node) { |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 183 | if (vport->port_no == port_no) |
| 184 | return vport; |
| 185 | } |
| 186 | return NULL; |
| 187 | } |
| 188 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 189 | /* Called with ovs_mutex. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 190 | static struct vport *new_vport(const struct vport_parms *parms) |
| 191 | { |
| 192 | struct vport *vport; |
| 193 | |
| 194 | vport = ovs_vport_add(parms); |
| 195 | if (!IS_ERR(vport)) { |
| 196 | struct datapath *dp = parms->dp; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 197 | struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 198 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 199 | hlist_add_head_rcu(&vport->dp_hash_node, head); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 200 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 201 | return vport; |
| 202 | } |
| 203 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 204 | void ovs_dp_detach_port(struct vport *p) |
| 205 | { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 206 | ASSERT_OVSL(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 207 | |
| 208 | /* First drop references to device. */ |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 209 | hlist_del_rcu(&p->dp_hash_node); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 210 | |
| 211 | /* Then destroy it. */ |
| 212 | ovs_vport_del(p); |
| 213 | } |
| 214 | |
| 215 | /* Must be called with rcu_read_lock. */ |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 216 | void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 217 | { |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 218 | const struct vport *p = OVS_CB(skb)->input_vport; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 219 | struct datapath *dp = p->dp; |
| 220 | struct sw_flow *flow; |
Lorand Jakab | d98612b | 2014-10-06 05:45:32 -0700 | [diff] [blame] | 221 | struct sw_flow_actions *sf_acts; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 222 | struct dp_stats_percpu *stats; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 223 | u64 *stats_counter; |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 224 | u32 n_mask_hit; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 225 | |
Shan Wei | 404f2f1 | 2012-11-13 09:52:25 +0800 | [diff] [blame] | 226 | stats = this_cpu_ptr(dp->stats_percpu); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 227 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 228 | /* Look up flow. */ |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 229 | flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 230 | if (unlikely(!flow)) { |
| 231 | struct dp_upcall_info upcall; |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 232 | int error; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 233 | |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 234 | memset(&upcall, 0, sizeof(upcall)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 235 | upcall.cmd = OVS_PACKET_CMD_MISS; |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 236 | upcall.portid = ovs_vport_find_upcall_portid(p, skb); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 237 | upcall.mru = OVS_CB(skb)->mru; |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 238 | error = ovs_dp_upcall(dp, skb, key, &upcall, 0); |
Li RongQing | c5eba0b | 2014-09-03 17:43:45 +0800 | [diff] [blame] | 239 | if (unlikely(error)) |
| 240 | kfree_skb(skb); |
| 241 | else |
| 242 | consume_skb(skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 243 | stats_counter = &stats->n_missed; |
| 244 | goto out; |
| 245 | } |
| 246 | |
Lorand Jakab | d98612b | 2014-10-06 05:45:32 -0700 | [diff] [blame] | 247 | ovs_flow_stats_update(flow, key->tp.flags, skb); |
| 248 | sf_acts = rcu_dereference(flow->sf_acts); |
| 249 | ovs_execute_actions(dp, skb, sf_acts, key); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 250 | |
Pravin B Shelar | e298e50 | 2013-10-29 17:22:21 -0700 | [diff] [blame] | 251 | stats_counter = &stats->n_hit; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 252 | |
| 253 | out: |
| 254 | /* Update datapath statistics. */ |
WANG Cong | df9d9fd | 2014-02-14 15:10:46 -0800 | [diff] [blame] | 255 | u64_stats_update_begin(&stats->syncp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 256 | (*stats_counter)++; |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 257 | stats->n_mask_hit += n_mask_hit; |
WANG Cong | df9d9fd | 2014-02-14 15:10:46 -0800 | [diff] [blame] | 258 | u64_stats_update_end(&stats->syncp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 261 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, |
Pravin B Shelar | e8eedb8 | 2014-11-06 06:57:27 -0800 | [diff] [blame] | 262 | const struct sw_flow_key *key, |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 263 | const struct dp_upcall_info *upcall_info, |
| 264 | uint32_t cutlen) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 265 | { |
| 266 | struct dp_stats_percpu *stats; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 267 | int err; |
| 268 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 269 | if (upcall_info->portid == 0) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 270 | err = -ENOTCONN; |
| 271 | goto err; |
| 272 | } |
| 273 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 274 | if (!skb_is_gso(skb)) |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 275 | err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 276 | else |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 277 | err = queue_gso_packets(dp, skb, key, upcall_info, cutlen); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 278 | if (err) |
| 279 | goto err; |
| 280 | |
| 281 | return 0; |
| 282 | |
| 283 | err: |
Shan Wei | 404f2f1 | 2012-11-13 09:52:25 +0800 | [diff] [blame] | 284 | stats = this_cpu_ptr(dp->stats_percpu); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 285 | |
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->n_lost++; |
WANG Cong | df9d9fd | 2014-02-14 15:10:46 -0800 | [diff] [blame] | 288 | u64_stats_update_end(&stats->syncp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 289 | |
| 290 | return err; |
| 291 | } |
| 292 | |
Thomas Graf | 8055a89 | 2013-12-13 15:22:20 +0100 | [diff] [blame] | 293 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb, |
Pravin B Shelar | e8eedb8 | 2014-11-06 06:57:27 -0800 | [diff] [blame] | 294 | const struct sw_flow_key *key, |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 295 | const struct dp_upcall_info *upcall_info, |
| 296 | uint32_t cutlen) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 297 | { |
Gustavo A. R. Silva | 2734166 | 2017-11-25 13:14:40 -0600 | [diff] [blame] | 298 | unsigned int gso_type = skb_shinfo(skb)->gso_type; |
Willem de Bruijn | 0c19f846 | 2017-11-21 10:22:25 -0500 | [diff] [blame] | 299 | struct sw_flow_key later_key; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 300 | struct sk_buff *segs, *nskb; |
| 301 | int err; |
| 302 | |
Konstantin Khlebnikov | 9207f9d | 2016-01-08 15:21:46 +0300 | [diff] [blame] | 303 | BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET); |
Thomas Graf | 09c5e60 | 2013-12-13 15:22:22 +0100 | [diff] [blame] | 304 | segs = __skb_gso_segment(skb, NETIF_F_SG, false); |
Pravin B Shelar | 92e5dfc | 2012-07-20 14:46:29 -0700 | [diff] [blame] | 305 | if (IS_ERR(segs)) |
| 306 | return PTR_ERR(segs); |
Florian Westphal | 330966e | 2014-10-20 13:49:17 +0200 | [diff] [blame] | 307 | if (segs == NULL) |
| 308 | return -EINVAL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 309 | |
Willem de Bruijn | 0c19f846 | 2017-11-21 10:22:25 -0500 | [diff] [blame] | 310 | if (gso_type & SKB_GSO_UDP) { |
| 311 | /* The initial flow key extracted by ovs_flow_key_extract() |
| 312 | * in this case is for a first fragment, so we need to |
| 313 | * properly mark later fragments. |
| 314 | */ |
| 315 | later_key = *key; |
| 316 | later_key.ip.frag = OVS_FRAG_TYPE_LATER; |
| 317 | } |
| 318 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 319 | /* Queue all of the segments. */ |
| 320 | skb = segs; |
| 321 | do { |
Willem de Bruijn | 0c19f846 | 2017-11-21 10:22:25 -0500 | [diff] [blame] | 322 | if (gso_type & SKB_GSO_UDP && skb != segs) |
| 323 | key = &later_key; |
| 324 | |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 325 | err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 326 | if (err) |
| 327 | break; |
| 328 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 329 | } while ((skb = skb->next)); |
| 330 | |
| 331 | /* Free all of the segments. */ |
| 332 | skb = segs; |
| 333 | do { |
| 334 | nskb = skb->next; |
| 335 | if (err) |
| 336 | kfree_skb(skb); |
| 337 | else |
| 338 | consume_skb(skb); |
| 339 | } while ((skb = nskb)); |
| 340 | return err; |
| 341 | } |
| 342 | |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 343 | static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info, |
Liping Zhang | 494bea3 | 2017-08-16 13:30:07 +0800 | [diff] [blame] | 344 | unsigned int hdrlen, int actions_attrlen) |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 345 | { |
| 346 | size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) |
Thomas Graf | bda56f1 | 2013-12-13 15:22:21 +0100 | [diff] [blame] | 347 | + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */ |
William Tu | b95e592 | 2016-06-20 07:26:17 -0700 | [diff] [blame] | 348 | + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */ |
| 349 | + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */ |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 350 | |
| 351 | /* OVS_PACKET_ATTR_USERDATA */ |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 352 | if (upcall_info->userdata) |
| 353 | size += NLA_ALIGN(upcall_info->userdata->nla_len); |
| 354 | |
| 355 | /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */ |
| 356 | if (upcall_info->egress_tun_info) |
| 357 | size += nla_total_size(ovs_tun_key_attr_size()); |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 358 | |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 359 | /* OVS_PACKET_ATTR_ACTIONS */ |
| 360 | if (upcall_info->actions_len) |
Liping Zhang | 494bea3 | 2017-08-16 13:30:07 +0800 | [diff] [blame] | 361 | size += nla_total_size(actions_attrlen); |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 362 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 363 | /* OVS_PACKET_ATTR_MRU */ |
| 364 | if (upcall_info->mru) |
| 365 | size += nla_total_size(sizeof(upcall_info->mru)); |
| 366 | |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 367 | return size; |
| 368 | } |
| 369 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 370 | static void pad_packet(struct datapath *dp, struct sk_buff *skb) |
| 371 | { |
| 372 | if (!(dp->user_features & OVS_DP_F_UNALIGNED)) { |
| 373 | size_t plen = NLA_ALIGN(skb->len) - skb->len; |
| 374 | |
| 375 | if (plen > 0) |
Johannes Berg | b080db5 | 2017-06-16 14:29:19 +0200 | [diff] [blame] | 376 | skb_put_zero(skb, plen); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Thomas Graf | 8055a89 | 2013-12-13 15:22:20 +0100 | [diff] [blame] | 380 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, |
Pravin B Shelar | e8eedb8 | 2014-11-06 06:57:27 -0800 | [diff] [blame] | 381 | const struct sw_flow_key *key, |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 382 | const struct dp_upcall_info *upcall_info, |
| 383 | uint32_t cutlen) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 384 | { |
| 385 | struct ovs_header *upcall; |
| 386 | struct sk_buff *nskb = NULL; |
Li RongQing | 4ee45ea | 2014-09-02 20:52:28 +0800 | [diff] [blame] | 387 | struct sk_buff *user_skb = NULL; /* to be queued to userspace */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 388 | struct nlattr *nla; |
Thomas Graf | 795449d | 2013-11-30 13:21:32 +0100 | [diff] [blame] | 389 | size_t len; |
Thomas Graf | bda56f1 | 2013-12-13 15:22:21 +0100 | [diff] [blame] | 390 | unsigned int hlen; |
Thomas Graf | 8055a89 | 2013-12-13 15:22:20 +0100 | [diff] [blame] | 391 | int err, dp_ifindex; |
| 392 | |
| 393 | dp_ifindex = get_dpifindex(dp); |
| 394 | if (!dp_ifindex) |
| 395 | return -ENODEV; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 396 | |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 397 | if (skb_vlan_tag_present(skb)) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 398 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 399 | if (!nskb) |
| 400 | return -ENOMEM; |
| 401 | |
Jiri Pirko | 5968250 | 2014-11-19 14:04:59 +0100 | [diff] [blame] | 402 | nskb = __vlan_hwaccel_push_inside(nskb); |
Dan Carpenter | 8aa51d6 | 2012-05-13 08:44:18 +0000 | [diff] [blame] | 403 | if (!nskb) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 404 | return -ENOMEM; |
| 405 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 406 | skb = nskb; |
| 407 | } |
| 408 | |
| 409 | if (nla_attr_size(skb->len) > USHRT_MAX) { |
| 410 | err = -EFBIG; |
| 411 | goto out; |
| 412 | } |
| 413 | |
Thomas Graf | bda56f1 | 2013-12-13 15:22:21 +0100 | [diff] [blame] | 414 | /* Complete checksum if needed */ |
| 415 | if (skb->ip_summed == CHECKSUM_PARTIAL && |
Davide Caratti | 7529390 | 2017-05-18 15:44:42 +0200 | [diff] [blame] | 416 | (err = skb_csum_hwoffload_help(skb, 0))) |
Thomas Graf | bda56f1 | 2013-12-13 15:22:21 +0100 | [diff] [blame] | 417 | goto out; |
| 418 | |
| 419 | /* Older versions of OVS user space enforce alignment of the last |
| 420 | * Netlink attribute to NLA_ALIGNTO which would require extensive |
| 421 | * padding logic. Only perform zerocopy if padding is not required. |
| 422 | */ |
| 423 | if (dp->user_features & OVS_DP_F_UNALIGNED) |
| 424 | hlen = skb_zerocopy_headlen(skb); |
| 425 | else |
| 426 | hlen = skb->len; |
| 427 | |
Liping Zhang | 494bea3 | 2017-08-16 13:30:07 +0800 | [diff] [blame] | 428 | len = upcall_msg_size(upcall_info, hlen - cutlen, |
| 429 | OVS_CB(skb)->acts_origlen); |
Florian Westphal | 551ddc0 | 2016-02-18 15:03:25 +0100 | [diff] [blame] | 430 | user_skb = genlmsg_new(len, GFP_ATOMIC); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 431 | if (!user_skb) { |
| 432 | err = -ENOMEM; |
| 433 | goto out; |
| 434 | } |
| 435 | |
| 436 | upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, |
| 437 | 0, upcall_info->cmd); |
Kangjie Lu | 6f19893 | 2019-03-14 23:20:16 -0500 | [diff] [blame] | 438 | if (!upcall) { |
| 439 | err = -EINVAL; |
| 440 | goto out; |
| 441 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 442 | upcall->dp_ifindex = dp_ifindex; |
| 443 | |
Joe Stringer | 5b4237b | 2015-01-21 16:42:48 -0800 | [diff] [blame] | 444 | err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb); |
Eelco Chaudron | a734d1f | 2019-05-02 16:12:38 -0400 | [diff] [blame] | 445 | if (err) |
| 446 | goto out; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 447 | |
| 448 | if (upcall_info->userdata) |
Ben Pfaff | 4490108 | 2013-02-15 17:29:22 -0800 | [diff] [blame] | 449 | __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, |
| 450 | nla_len(upcall_info->userdata), |
| 451 | nla_data(upcall_info->userdata)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 452 | |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 453 | if (upcall_info->egress_tun_info) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 454 | nla = nla_nest_start_noflag(user_skb, |
| 455 | OVS_PACKET_ATTR_EGRESS_TUN_KEY); |
Kangjie Lu | 0fff9bd | 2019-03-15 01:11:22 -0500 | [diff] [blame] | 456 | if (!nla) { |
| 457 | err = -EMSGSIZE; |
| 458 | goto out; |
| 459 | } |
Pravin B Shelar | fc4099f | 2015-10-22 18:17:16 -0700 | [diff] [blame] | 460 | err = ovs_nla_put_tunnel_info(user_skb, |
| 461 | upcall_info->egress_tun_info); |
Eelco Chaudron | a734d1f | 2019-05-02 16:12:38 -0400 | [diff] [blame] | 462 | if (err) |
| 463 | goto out; |
| 464 | |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 465 | nla_nest_end(user_skb, nla); |
| 466 | } |
| 467 | |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 468 | if (upcall_info->actions_len) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 469 | nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS); |
Kangjie Lu | 0fff9bd | 2019-03-15 01:11:22 -0500 | [diff] [blame] | 470 | if (!nla) { |
| 471 | err = -EMSGSIZE; |
| 472 | goto out; |
| 473 | } |
Neil McKee | ccea744 | 2015-05-26 20:59:43 -0700 | [diff] [blame] | 474 | err = ovs_nla_put_actions(upcall_info->actions, |
| 475 | upcall_info->actions_len, |
| 476 | user_skb); |
| 477 | if (!err) |
| 478 | nla_nest_end(user_skb, nla); |
| 479 | else |
| 480 | nla_nest_cancel(user_skb, nla); |
| 481 | } |
| 482 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 483 | /* Add OVS_PACKET_ATTR_MRU */ |
| 484 | if (upcall_info->mru) { |
| 485 | if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU, |
| 486 | upcall_info->mru)) { |
| 487 | err = -ENOBUFS; |
| 488 | goto out; |
| 489 | } |
| 490 | pad_packet(dp, user_skb); |
| 491 | } |
| 492 | |
William Tu | b95e592 | 2016-06-20 07:26:17 -0700 | [diff] [blame] | 493 | /* Add OVS_PACKET_ATTR_LEN when packet is truncated */ |
| 494 | if (cutlen > 0) { |
| 495 | if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN, |
| 496 | skb->len)) { |
| 497 | err = -ENOBUFS; |
| 498 | goto out; |
| 499 | } |
| 500 | pad_packet(dp, user_skb); |
| 501 | } |
| 502 | |
Thomas Graf | bda56f1 | 2013-12-13 15:22:21 +0100 | [diff] [blame] | 503 | /* Only reserve room for attribute header, packet data is added |
| 504 | * in skb_zerocopy() */ |
| 505 | if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) { |
| 506 | err = -ENOBUFS; |
| 507 | goto out; |
| 508 | } |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 509 | nla->nla_len = nla_attr_size(skb->len - cutlen); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 510 | |
William Tu | f2a4d08 | 2016-06-10 11:49:33 -0700 | [diff] [blame] | 511 | err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen); |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 512 | if (err) |
| 513 | goto out; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 514 | |
Thomas Graf | aea0bb4 | 2014-01-14 16:27:49 +0000 | [diff] [blame] | 515 | /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 516 | pad_packet(dp, user_skb); |
Thomas Graf | aea0bb4 | 2014-01-14 16:27:49 +0000 | [diff] [blame] | 517 | |
Thomas Graf | bda56f1 | 2013-12-13 15:22:21 +0100 | [diff] [blame] | 518 | ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len; |
| 519 | |
Thomas Graf | 8055a89 | 2013-12-13 15:22:20 +0100 | [diff] [blame] | 520 | 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] | 521 | user_skb = NULL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 522 | out: |
Zoltan Kiss | 36d5fe6 | 2014-03-26 22:37:45 +0000 | [diff] [blame] | 523 | if (err) |
| 524 | skb_tx_error(skb); |
Li RongQing | 4ee45ea | 2014-09-02 20:52:28 +0800 | [diff] [blame] | 525 | kfree_skb(user_skb); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 526 | kfree_skb(nskb); |
| 527 | return err; |
| 528 | } |
| 529 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 530 | static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) |
| 531 | { |
| 532 | struct ovs_header *ovs_header = info->userhdr; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 533 | struct net *net = sock_net(skb->sk); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 534 | struct nlattr **a = info->attrs; |
| 535 | struct sw_flow_actions *acts; |
| 536 | struct sk_buff *packet; |
| 537 | struct sw_flow *flow; |
Lorand Jakab | d98612b | 2014-10-06 05:45:32 -0700 | [diff] [blame] | 538 | struct sw_flow_actions *sf_acts; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 539 | struct datapath *dp; |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 540 | struct vport *input_vport; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 541 | u16 mru = 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 542 | int len; |
| 543 | int err; |
Thomas Graf | 1ba3980 | 2015-01-14 13:56:19 +0000 | [diff] [blame] | 544 | bool log = !a[OVS_PACKET_ATTR_PROBE]; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 545 | |
| 546 | err = -EINVAL; |
| 547 | if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || |
Thomas Graf | dded45fc | 2013-03-29 14:46:47 +0100 | [diff] [blame] | 548 | !a[OVS_PACKET_ATTR_ACTIONS]) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 549 | goto err; |
| 550 | |
| 551 | len = nla_len(a[OVS_PACKET_ATTR_PACKET]); |
| 552 | packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); |
| 553 | err = -ENOMEM; |
| 554 | if (!packet) |
| 555 | goto err; |
| 556 | skb_reserve(packet, NET_IP_ALIGN); |
| 557 | |
Thomas Graf | 32686a9 | 2013-03-29 14:46:48 +0100 | [diff] [blame] | 558 | nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 559 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 560 | /* Set packet's mru */ |
| 561 | if (a[OVS_PACKET_ATTR_MRU]) { |
| 562 | mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]); |
| 563 | packet->ignore_df = 1; |
| 564 | } |
| 565 | OVS_CB(packet)->mru = mru; |
| 566 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 567 | /* Build an sw_flow for sending this packet. */ |
Jarno Rajahalme | 23dabf8 | 2014-03-27 12:35:23 -0700 | [diff] [blame] | 568 | flow = ovs_flow_alloc(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 569 | err = PTR_ERR(flow); |
| 570 | if (IS_ERR(flow)) |
| 571 | goto err_kfree_skb; |
| 572 | |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 573 | err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY], |
| 574 | packet, &flow->key, log); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 575 | if (err) |
| 576 | goto err_flow_free; |
| 577 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 578 | err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS], |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 579 | &flow->key, &acts, log); |
Pravin B Shelar | 74f84a5 | 2013-06-17 17:50:12 -0700 | [diff] [blame] | 580 | if (err) |
| 581 | goto err_flow_free; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 582 | |
Jesse Gross | f579668 | 2014-10-03 15:35:33 -0700 | [diff] [blame] | 583 | rcu_assign_pointer(flow->sf_acts, acts); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 584 | packet->priority = flow->key.phy.priority; |
Ansis Atteka | 39c7caeb | 2012-11-26 11:24:11 -0800 | [diff] [blame] | 585 | packet->mark = flow->key.phy.skb_mark; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 586 | |
| 587 | rcu_read_lock(); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 588 | dp = get_dp_rcu(net, ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 589 | err = -ENODEV; |
| 590 | if (!dp) |
| 591 | goto err_unlock; |
| 592 | |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 593 | input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port); |
| 594 | if (!input_vport) |
| 595 | input_vport = ovs_vport_rcu(dp, OVSP_LOCAL); |
| 596 | |
| 597 | if (!input_vport) |
| 598 | goto err_unlock; |
| 599 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 600 | packet->dev = input_vport->dev; |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 601 | OVS_CB(packet)->input_vport = input_vport; |
Lorand Jakab | d98612b | 2014-10-06 05:45:32 -0700 | [diff] [blame] | 602 | sf_acts = rcu_dereference(flow->sf_acts); |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 603 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 604 | local_bh_disable(); |
Lorand Jakab | d98612b | 2014-10-06 05:45:32 -0700 | [diff] [blame] | 605 | err = ovs_execute_actions(dp, packet, sf_acts, &flow->key); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 606 | local_bh_enable(); |
| 607 | rcu_read_unlock(); |
| 608 | |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 609 | ovs_flow_free(flow, false); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 610 | return err; |
| 611 | |
| 612 | err_unlock: |
| 613 | rcu_read_unlock(); |
| 614 | err_flow_free: |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 615 | ovs_flow_free(flow, false); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 616 | err_kfree_skb: |
| 617 | kfree_skb(packet); |
| 618 | err: |
| 619 | return err; |
| 620 | } |
| 621 | |
| 622 | static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { |
Thomas Graf | dded45fc | 2013-03-29 14:46:47 +0100 | [diff] [blame] | 623 | [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 624 | [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, |
| 625 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, |
Thomas Graf | 1ba3980 | 2015-01-14 13:56:19 +0000 | [diff] [blame] | 626 | [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG }, |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 627 | [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 }, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 628 | }; |
| 629 | |
Johannes Berg | 4534de8 | 2013-11-14 17:14:46 +0100 | [diff] [blame] | 630 | static const struct genl_ops dp_packet_genl_ops[] = { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 631 | { .cmd = OVS_PACKET_CMD_EXECUTE, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 632 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 633 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 634 | .doit = ovs_packet_cmd_execute |
| 635 | } |
| 636 | }; |
| 637 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 638 | static struct genl_family dp_packet_genl_family __ro_after_init = { |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 639 | .hdrsize = sizeof(struct ovs_header), |
| 640 | .name = OVS_PACKET_FAMILY, |
| 641 | .version = OVS_PACKET_VERSION, |
| 642 | .maxattr = OVS_PACKET_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 643 | .policy = packet_policy, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 644 | .netnsok = true, |
| 645 | .parallel_ops = true, |
| 646 | .ops = dp_packet_genl_ops, |
| 647 | .n_ops = ARRAY_SIZE(dp_packet_genl_ops), |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 648 | .module = THIS_MODULE, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 649 | }; |
| 650 | |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 651 | static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats, |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 652 | struct ovs_dp_megaflow_stats *mega_stats) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 653 | { |
| 654 | int i; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 655 | |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 656 | memset(mega_stats, 0, sizeof(*mega_stats)); |
| 657 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 658 | stats->n_flows = ovs_flow_tbl_count(&dp->table); |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 659 | mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 660 | |
| 661 | stats->n_hit = stats->n_missed = stats->n_lost = 0; |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 662 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 663 | for_each_possible_cpu(i) { |
| 664 | const struct dp_stats_percpu *percpu_stats; |
| 665 | struct dp_stats_percpu local_stats; |
| 666 | unsigned int start; |
| 667 | |
| 668 | percpu_stats = per_cpu_ptr(dp->stats_percpu, i); |
| 669 | |
| 670 | do { |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 671 | start = u64_stats_fetch_begin_irq(&percpu_stats->syncp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 672 | local_stats = *percpu_stats; |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 673 | } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 674 | |
| 675 | stats->n_hit += local_stats.n_hit; |
| 676 | stats->n_missed += local_stats.n_missed; |
| 677 | stats->n_lost += local_stats.n_lost; |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 678 | mega_stats->n_mask_hit += local_stats.n_mask_hit; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 682 | static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags) |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 683 | { |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 684 | return ovs_identifier_is_ufid(sfid) && |
| 685 | !(ufid_flags & OVS_UFID_F_OMIT_KEY); |
| 686 | } |
| 687 | |
| 688 | static bool should_fill_mask(uint32_t ufid_flags) |
| 689 | { |
| 690 | return !(ufid_flags & OVS_UFID_F_OMIT_MASK); |
| 691 | } |
| 692 | |
| 693 | static bool should_fill_actions(uint32_t ufid_flags) |
| 694 | { |
| 695 | return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS); |
| 696 | } |
| 697 | |
| 698 | static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts, |
| 699 | const struct sw_flow_id *sfid, |
| 700 | uint32_t ufid_flags) |
| 701 | { |
| 702 | size_t len = NLMSG_ALIGN(sizeof(struct ovs_header)); |
| 703 | |
| 704 | /* OVS_FLOW_ATTR_UFID */ |
| 705 | if (sfid && ovs_identifier_is_ufid(sfid)) |
| 706 | len += nla_total_size(sfid->ufid_len); |
| 707 | |
| 708 | /* OVS_FLOW_ATTR_KEY */ |
| 709 | if (!sfid || should_fill_key(sfid, ufid_flags)) |
| 710 | len += nla_total_size(ovs_key_attr_size()); |
| 711 | |
| 712 | /* OVS_FLOW_ATTR_MASK */ |
| 713 | if (should_fill_mask(ufid_flags)) |
| 714 | len += nla_total_size(ovs_key_attr_size()); |
| 715 | |
| 716 | /* OVS_FLOW_ATTR_ACTIONS */ |
| 717 | if (should_fill_actions(ufid_flags)) |
Joe Stringer | 8e2fed1 | 2015-08-26 11:31:44 -0700 | [diff] [blame] | 718 | len += nla_total_size(acts->orig_len); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 719 | |
| 720 | return len |
Nicolas Dichtel | 66c7a5e | 2016-04-26 10:06:15 +0200 | [diff] [blame] | 721 | + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 722 | + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ |
Nicolas Dichtel | 66c7a5e | 2016-04-26 10:06:15 +0200 | [diff] [blame] | 723 | + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */ |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 724 | } |
| 725 | |
Jarno Rajahalme | bb6f9a7 | 2014-05-05 11:32:17 -0700 | [diff] [blame] | 726 | /* Called with ovs_mutex or RCU read lock. */ |
Joe Stringer | ca7105f | 2014-09-08 13:09:37 -0700 | [diff] [blame] | 727 | static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow, |
| 728 | struct sk_buff *skb) |
| 729 | { |
| 730 | struct ovs_flow_stats stats; |
| 731 | __be16 tcp_flags; |
| 732 | unsigned long used; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 733 | |
Pravin B Shelar | e298e50 | 2013-10-29 17:22:21 -0700 | [diff] [blame] | 734 | ovs_flow_stats_get(flow, &stats, &used, &tcp_flags); |
Jarno Rajahalme | 0e9796b | 2014-05-05 14:28:07 -0700 | [diff] [blame] | 735 | |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 736 | if (used && |
Nicolas Dichtel | 0238b72 | 2016-04-25 10:25:17 +0200 | [diff] [blame] | 737 | nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used), |
| 738 | OVS_FLOW_ATTR_PAD)) |
Joe Stringer | ca7105f | 2014-09-08 13:09:37 -0700 | [diff] [blame] | 739 | return -EMSGSIZE; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 740 | |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 741 | if (stats.n_packets && |
Nicolas Dichtel | 66c7a5e | 2016-04-26 10:06:15 +0200 | [diff] [blame] | 742 | nla_put_64bit(skb, OVS_FLOW_ATTR_STATS, |
| 743 | sizeof(struct ovs_flow_stats), &stats, |
| 744 | OVS_FLOW_ATTR_PAD)) |
Joe Stringer | ca7105f | 2014-09-08 13:09:37 -0700 | [diff] [blame] | 745 | return -EMSGSIZE; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 746 | |
Pravin B Shelar | e298e50 | 2013-10-29 17:22:21 -0700 | [diff] [blame] | 747 | if ((u8)ntohs(tcp_flags) && |
| 748 | nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags))) |
Joe Stringer | ca7105f | 2014-09-08 13:09:37 -0700 | [diff] [blame] | 749 | return -EMSGSIZE; |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* Called with ovs_mutex or RCU read lock. */ |
| 755 | static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow, |
| 756 | struct sk_buff *skb, int skb_orig_len) |
| 757 | { |
| 758 | struct nlattr *start; |
| 759 | int err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 760 | |
| 761 | /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if |
| 762 | * this is the first flow to be dumped into 'skb'. This is unusual for |
| 763 | * Netlink but individual action lists can be longer than |
| 764 | * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. |
| 765 | * The userspace caller can always fetch the actions separately if it |
| 766 | * really wants them. (Most userspace callers in fact don't care.) |
| 767 | * |
| 768 | * This can only fail for dump operations because the skb is always |
| 769 | * properly sized for single flows. |
| 770 | */ |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 771 | start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS); |
Pravin B Shelar | 74f84a5 | 2013-06-17 17:50:12 -0700 | [diff] [blame] | 772 | if (start) { |
Pravin B Shelar | d57170b | 2013-07-30 15:39:39 -0700 | [diff] [blame] | 773 | const struct sw_flow_actions *sf_acts; |
| 774 | |
Jesse Gross | 663efa3 | 2013-12-03 10:58:53 -0800 | [diff] [blame] | 775 | sf_acts = rcu_dereference_ovsl(flow->sf_acts); |
Pravin B Shelar | e644571 | 2013-10-03 18:16:47 -0700 | [diff] [blame] | 776 | err = ovs_nla_put_actions(sf_acts->actions, |
| 777 | sf_acts->actions_len, skb); |
Jarno Rajahalme | 0e9796b | 2014-05-05 14:28:07 -0700 | [diff] [blame] | 778 | |
Pravin B Shelar | 74f84a5 | 2013-06-17 17:50:12 -0700 | [diff] [blame] | 779 | if (!err) |
| 780 | nla_nest_end(skb, start); |
| 781 | else { |
| 782 | if (skb_orig_len) |
Joe Stringer | ca7105f | 2014-09-08 13:09:37 -0700 | [diff] [blame] | 783 | return err; |
Pravin B Shelar | 74f84a5 | 2013-06-17 17:50:12 -0700 | [diff] [blame] | 784 | |
| 785 | nla_nest_cancel(skb, start); |
| 786 | } |
Joe Stringer | ca7105f | 2014-09-08 13:09:37 -0700 | [diff] [blame] | 787 | } else if (skb_orig_len) { |
| 788 | return -EMSGSIZE; |
| 789 | } |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | /* Called with ovs_mutex or RCU read lock. */ |
| 795 | static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex, |
| 796 | struct sk_buff *skb, u32 portid, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 797 | u32 seq, u32 flags, u8 cmd, u32 ufid_flags) |
Joe Stringer | ca7105f | 2014-09-08 13:09:37 -0700 | [diff] [blame] | 798 | { |
| 799 | const int skb_orig_len = skb->len; |
| 800 | struct ovs_header *ovs_header; |
| 801 | int err; |
| 802 | |
| 803 | ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, |
| 804 | flags, cmd); |
| 805 | if (!ovs_header) |
| 806 | return -EMSGSIZE; |
| 807 | |
| 808 | ovs_header->dp_ifindex = dp_ifindex; |
| 809 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 810 | err = ovs_nla_put_identifier(flow, skb); |
Joe Stringer | 5b4237b | 2015-01-21 16:42:48 -0800 | [diff] [blame] | 811 | if (err) |
| 812 | goto error; |
| 813 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 814 | if (should_fill_key(&flow->id, ufid_flags)) { |
| 815 | err = ovs_nla_put_masked_key(flow, skb); |
| 816 | if (err) |
| 817 | goto error; |
| 818 | } |
| 819 | |
| 820 | if (should_fill_mask(ufid_flags)) { |
| 821 | err = ovs_nla_put_mask(flow, skb); |
| 822 | if (err) |
| 823 | goto error; |
| 824 | } |
Joe Stringer | ca7105f | 2014-09-08 13:09:37 -0700 | [diff] [blame] | 825 | |
| 826 | err = ovs_flow_cmd_fill_stats(flow, skb); |
| 827 | if (err) |
| 828 | goto error; |
| 829 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 830 | if (should_fill_actions(ufid_flags)) { |
| 831 | err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len); |
| 832 | if (err) |
| 833 | goto error; |
| 834 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 835 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 836 | genlmsg_end(skb, ovs_header); |
| 837 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 838 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 839 | error: |
| 840 | genlmsg_cancel(skb, ovs_header); |
| 841 | return err; |
| 842 | } |
| 843 | |
Jarno Rajahalme | 0e9796b | 2014-05-05 14:28:07 -0700 | [diff] [blame] | 844 | /* May not be called with RCU read lock. */ |
| 845 | static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 846 | const struct sw_flow_id *sfid, |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 847 | struct genl_info *info, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 848 | bool always, |
| 849 | uint32_t ufid_flags) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 850 | { |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 851 | struct sk_buff *skb; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 852 | size_t len; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 853 | |
Samuel Gauthier | 9b67aa4 | 2014-09-18 10:31:04 +0200 | [diff] [blame] | 854 | if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0)) |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 855 | return NULL; |
| 856 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 857 | len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags); |
Florian Westphal | 551ddc0 | 2016-02-18 15:03:25 +0100 | [diff] [blame] | 858 | skb = genlmsg_new(len, GFP_KERNEL); |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 859 | if (!skb) |
| 860 | return ERR_PTR(-ENOMEM); |
| 861 | |
| 862 | return skb; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Jarno Rajahalme | 0e9796b | 2014-05-05 14:28:07 -0700 | [diff] [blame] | 865 | /* Called with ovs_mutex. */ |
| 866 | static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow, |
| 867 | int dp_ifindex, |
| 868 | struct genl_info *info, u8 cmd, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 869 | bool always, u32 ufid_flags) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 870 | { |
| 871 | struct sk_buff *skb; |
| 872 | int retval; |
| 873 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 874 | skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), |
| 875 | &flow->id, info, always, ufid_flags); |
Himangi Saraogi | d0e992a | 2014-07-27 12:37:46 +0530 | [diff] [blame] | 876 | if (IS_ERR_OR_NULL(skb)) |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 877 | return skb; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 878 | |
Jarno Rajahalme | 0e9796b | 2014-05-05 14:28:07 -0700 | [diff] [blame] | 879 | retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb, |
| 880 | info->snd_portid, info->snd_seq, 0, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 881 | cmd, ufid_flags); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 882 | BUG_ON(retval < 0); |
| 883 | return skb; |
| 884 | } |
| 885 | |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 886 | 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] | 887 | { |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 888 | struct net *net = sock_net(skb->sk); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 889 | struct nlattr **a = info->attrs; |
| 890 | struct ovs_header *ovs_header = info->userhdr; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 891 | struct sw_flow *flow = NULL, *new_flow; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 892 | struct sw_flow_mask mask; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 893 | struct sk_buff *reply; |
| 894 | struct datapath *dp; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 895 | struct sw_flow_actions *acts; |
| 896 | struct sw_flow_match match; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 897 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 898 | int error; |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 899 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 900 | |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 901 | /* Must have key and actions. */ |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 902 | error = -EINVAL; |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 903 | if (!a[OVS_FLOW_ATTR_KEY]) { |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 904 | OVS_NLERR(log, "Flow key attr not present in new flow."); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 905 | goto error; |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 906 | } |
| 907 | if (!a[OVS_FLOW_ATTR_ACTIONS]) { |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 908 | OVS_NLERR(log, "Flow actions attr not present in new flow."); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 909 | goto error; |
Jesse Gross | 426cda5 | 2014-10-06 05:08:38 -0700 | [diff] [blame] | 910 | } |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 911 | |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 912 | /* Most of the time we need to allocate a new flow, do it before |
| 913 | * locking. |
| 914 | */ |
| 915 | new_flow = ovs_flow_alloc(); |
| 916 | if (IS_ERR(new_flow)) { |
| 917 | error = PTR_ERR(new_flow); |
| 918 | goto error; |
| 919 | } |
| 920 | |
| 921 | /* Extract key. */ |
pravin shelar | 2279994 | 2016-09-19 13:51:00 -0700 | [diff] [blame] | 922 | ovs_match_init(&match, &new_flow->key, false, &mask); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 923 | error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 924 | a[OVS_FLOW_ATTR_MASK], log); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 925 | if (error) |
| 926 | goto err_kfree_flow; |
| 927 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 928 | /* Extract flow identifier. */ |
| 929 | error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID], |
pravin shelar | 190aa3e7 | 2016-09-19 13:50:59 -0700 | [diff] [blame] | 930 | &new_flow->key, log); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 931 | if (error) |
| 932 | goto err_kfree_flow; |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 933 | |
pravin shelar | 190aa3e7 | 2016-09-19 13:50:59 -0700 | [diff] [blame] | 934 | /* unmasked key is needed to match when ufid is not used. */ |
| 935 | if (ovs_identifier_is_key(&new_flow->id)) |
| 936 | match.key = new_flow->id.unmasked_key; |
| 937 | |
| 938 | ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask); |
| 939 | |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 940 | /* Validate actions. */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 941 | error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS], |
| 942 | &new_flow->key, &acts, log); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 943 | if (error) { |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 944 | OVS_NLERR(log, "Flow actions may not be safe on all matching packets."); |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 945 | goto err_kfree_flow; |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 948 | reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false, |
| 949 | ufid_flags); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 950 | if (IS_ERR(reply)) { |
| 951 | error = PTR_ERR(reply); |
| 952 | goto err_kfree_acts; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | ovs_lock(); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 956 | dp = get_dp(net, ovs_header->dp_ifindex); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 957 | if (unlikely(!dp)) { |
| 958 | error = -ENODEV; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 959 | goto err_unlock_ovs; |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 960 | } |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 961 | |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 962 | /* Check if this is a duplicate flow */ |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 963 | if (ovs_identifier_is_ufid(&new_flow->id)) |
| 964 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id); |
| 965 | if (!flow) |
pravin shelar | 190aa3e7 | 2016-09-19 13:50:59 -0700 | [diff] [blame] | 966 | flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 967 | if (likely(!flow)) { |
| 968 | rcu_assign_pointer(new_flow->sf_acts, acts); |
| 969 | |
| 970 | /* Put flow in bucket. */ |
| 971 | error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask); |
| 972 | if (unlikely(error)) { |
| 973 | acts = NULL; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 974 | goto err_unlock_ovs; |
| 975 | } |
| 976 | |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 977 | if (unlikely(reply)) { |
| 978 | error = ovs_flow_cmd_fill_info(new_flow, |
| 979 | ovs_header->dp_ifindex, |
| 980 | reply, info->snd_portid, |
| 981 | info->snd_seq, 0, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 982 | OVS_FLOW_CMD_NEW, |
| 983 | ufid_flags); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 984 | BUG_ON(error < 0); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 985 | } |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 986 | ovs_unlock(); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 987 | } else { |
| 988 | struct sw_flow_actions *old_acts; |
| 989 | |
| 990 | /* Bail out if we're not allowed to modify an existing flow. |
| 991 | * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL |
| 992 | * because Generic Netlink treats the latter as a dump |
| 993 | * request. We also accept NLM_F_EXCL in case that bug ever |
| 994 | * gets fixed. |
| 995 | */ |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 996 | if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE |
| 997 | | NLM_F_EXCL))) { |
| 998 | error = -EEXIST; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 999 | goto err_unlock_ovs; |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1000 | } |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1001 | /* The flow identifier has to be the same for flow updates. |
| 1002 | * Look for any overlapping flow. |
| 1003 | */ |
| 1004 | if (unlikely(!ovs_flow_cmp(flow, &match))) { |
| 1005 | if (ovs_identifier_is_key(&flow->id)) |
| 1006 | flow = ovs_flow_tbl_lookup_exact(&dp->table, |
| 1007 | &match); |
| 1008 | else /* UFID matches but key is different */ |
| 1009 | flow = NULL; |
Alex Wang | 4a46b24 | 2014-06-30 20:30:29 -0700 | [diff] [blame] | 1010 | if (!flow) { |
| 1011 | error = -ENOENT; |
| 1012 | goto err_unlock_ovs; |
| 1013 | } |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1014 | } |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1015 | /* Update actions. */ |
| 1016 | old_acts = ovsl_dereference(flow->sf_acts); |
| 1017 | rcu_assign_pointer(flow->sf_acts, acts); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1018 | |
| 1019 | if (unlikely(reply)) { |
| 1020 | error = ovs_flow_cmd_fill_info(flow, |
| 1021 | ovs_header->dp_ifindex, |
| 1022 | reply, info->snd_portid, |
| 1023 | info->snd_seq, 0, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1024 | OVS_FLOW_CMD_NEW, |
| 1025 | ufid_flags); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1026 | BUG_ON(error < 0); |
| 1027 | } |
| 1028 | ovs_unlock(); |
| 1029 | |
Thomas Graf | 34ae932 | 2015-07-21 10:44:03 +0200 | [diff] [blame] | 1030 | ovs_nla_free_flow_actions_rcu(old_acts); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1031 | ovs_flow_free(new_flow, false); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1034 | if (reply) |
| 1035 | ovs_notify(&dp_flow_genl_family, reply, info); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1036 | return 0; |
| 1037 | |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1038 | err_unlock_ovs: |
| 1039 | ovs_unlock(); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1040 | kfree_skb(reply); |
| 1041 | err_kfree_acts: |
Thomas Graf | 34ae932 | 2015-07-21 10:44:03 +0200 | [diff] [blame] | 1042 | ovs_nla_free_flow_actions(acts); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1043 | err_kfree_flow: |
| 1044 | ovs_flow_free(new_flow, false); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1045 | error: |
| 1046 | return error; |
| 1047 | } |
| 1048 | |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 1049 | /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 1050 | static struct sw_flow_actions *get_flow_actions(struct net *net, |
| 1051 | const struct nlattr *a, |
Jesse Gross | 6b205b2 | 2014-10-03 15:35:32 -0700 | [diff] [blame] | 1052 | const struct sw_flow_key *key, |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 1053 | const struct sw_flow_mask *mask, |
| 1054 | bool log) |
Jesse Gross | 6b205b2 | 2014-10-03 15:35:32 -0700 | [diff] [blame] | 1055 | { |
| 1056 | struct sw_flow_actions *acts; |
| 1057 | struct sw_flow_key masked_key; |
| 1058 | int error; |
| 1059 | |
Jesse Gross | ae5f2fb | 2015-09-21 20:21:20 -0700 | [diff] [blame] | 1060 | ovs_flow_mask_key(&masked_key, key, true, mask); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 1061 | error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log); |
Jesse Gross | 6b205b2 | 2014-10-03 15:35:32 -0700 | [diff] [blame] | 1062 | if (error) { |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 1063 | OVS_NLERR(log, |
| 1064 | "Actions may not be safe on all matching packets"); |
Jesse Gross | 6b205b2 | 2014-10-03 15:35:32 -0700 | [diff] [blame] | 1065 | return ERR_PTR(error); |
| 1066 | } |
| 1067 | |
| 1068 | return acts; |
| 1069 | } |
| 1070 | |
Tonghao Zhang | 9cc9a5c | 2017-06-29 17:27:44 -0700 | [diff] [blame] | 1071 | /* Factor out match-init and action-copy to avoid |
| 1072 | * "Wframe-larger-than=1024" warning. Because mask is only |
| 1073 | * used to get actions, we new a function to save some |
| 1074 | * stack space. |
| 1075 | * |
| 1076 | * If there are not key and action attrs, we return 0 |
| 1077 | * directly. In the case, the caller will also not use the |
| 1078 | * match as before. If there is action attr, we try to get |
| 1079 | * actions and save them to *acts. Before returning from |
| 1080 | * the function, we reset the match->mask pointer. Because |
| 1081 | * we should not to return match object with dangling reference |
| 1082 | * to mask. |
| 1083 | * */ |
| 1084 | static int ovs_nla_init_match_and_action(struct net *net, |
| 1085 | struct sw_flow_match *match, |
| 1086 | struct sw_flow_key *key, |
| 1087 | struct nlattr **a, |
| 1088 | struct sw_flow_actions **acts, |
| 1089 | bool log) |
| 1090 | { |
| 1091 | struct sw_flow_mask mask; |
| 1092 | int error = 0; |
| 1093 | |
| 1094 | if (a[OVS_FLOW_ATTR_KEY]) { |
| 1095 | ovs_match_init(match, key, true, &mask); |
| 1096 | error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY], |
| 1097 | a[OVS_FLOW_ATTR_MASK], log); |
| 1098 | if (error) |
| 1099 | goto error; |
| 1100 | } |
| 1101 | |
| 1102 | if (a[OVS_FLOW_ATTR_ACTIONS]) { |
| 1103 | if (!a[OVS_FLOW_ATTR_KEY]) { |
| 1104 | OVS_NLERR(log, |
| 1105 | "Flow key attribute not present in set flow."); |
Christophe JAILLET | 5829e62 | 2017-09-11 21:56:20 +0200 | [diff] [blame] | 1106 | error = -EINVAL; |
| 1107 | goto error; |
Tonghao Zhang | 9cc9a5c | 2017-06-29 17:27:44 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key, |
| 1111 | &mask, log); |
| 1112 | if (IS_ERR(*acts)) { |
| 1113 | error = PTR_ERR(*acts); |
| 1114 | goto error; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /* On success, error is 0. */ |
| 1119 | error: |
| 1120 | match->mask = NULL; |
| 1121 | return error; |
| 1122 | } |
| 1123 | |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1124 | static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info) |
| 1125 | { |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 1126 | struct net *net = sock_net(skb->sk); |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1127 | struct nlattr **a = info->attrs; |
| 1128 | struct ovs_header *ovs_header = info->userhdr; |
Jesse Gross | 6b205b2 | 2014-10-03 15:35:32 -0700 | [diff] [blame] | 1129 | struct sw_flow_key key; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1130 | struct sw_flow *flow; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1131 | struct sk_buff *reply = NULL; |
| 1132 | struct datapath *dp; |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1133 | struct sw_flow_actions *old_acts = NULL, *acts = NULL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 1134 | struct sw_flow_match match; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1135 | struct sw_flow_id sfid; |
| 1136 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); |
Samuel Gauthier | 6f15cdb | 2016-03-10 17:14:59 +0100 | [diff] [blame] | 1137 | int error = 0; |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 1138 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1139 | bool ufid_present; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1140 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1141 | ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log); |
Tonghao Zhang | 9cc9a5c | 2017-06-29 17:27:44 -0700 | [diff] [blame] | 1142 | if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) { |
Samuel Gauthier | 6f15cdb | 2016-03-10 17:14:59 +0100 | [diff] [blame] | 1143 | OVS_NLERR(log, |
| 1144 | "Flow set message rejected, Key attribute missing."); |
Tonghao Zhang | 9cc9a5c | 2017-06-29 17:27:44 -0700 | [diff] [blame] | 1145 | return -EINVAL; |
Samuel Gauthier | 6f15cdb | 2016-03-10 17:14:59 +0100 | [diff] [blame] | 1146 | } |
Tonghao Zhang | 9cc9a5c | 2017-06-29 17:27:44 -0700 | [diff] [blame] | 1147 | |
| 1148 | error = ovs_nla_init_match_and_action(net, &match, &key, a, |
| 1149 | &acts, log); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1150 | if (error) |
| 1151 | goto error; |
| 1152 | |
Tonghao Zhang | 9cc9a5c | 2017-06-29 17:27:44 -0700 | [diff] [blame] | 1153 | if (acts) { |
Pravin B Shelar | 2fdb957 | 2014-10-19 11:19:51 -0700 | [diff] [blame] | 1154 | /* Can allocate before locking if have acts. */ |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1155 | reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false, |
| 1156 | ufid_flags); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1157 | if (IS_ERR(reply)) { |
| 1158 | error = PTR_ERR(reply); |
| 1159 | goto err_kfree_acts; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 1160 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1163 | ovs_lock(); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 1164 | dp = get_dp(net, ovs_header->dp_ifindex); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1165 | if (unlikely(!dp)) { |
| 1166 | error = -ENODEV; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1167 | goto err_unlock_ovs; |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1168 | } |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1169 | /* Check that the flow exists. */ |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1170 | if (ufid_present) |
| 1171 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid); |
| 1172 | else |
| 1173 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1174 | if (unlikely(!flow)) { |
| 1175 | error = -ENOENT; |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1176 | goto err_unlock_ovs; |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1177 | } |
Alex Wang | 4a46b24 | 2014-06-30 20:30:29 -0700 | [diff] [blame] | 1178 | |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1179 | /* Update actions, if present. */ |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1180 | if (likely(acts)) { |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1181 | old_acts = ovsl_dereference(flow->sf_acts); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1182 | rcu_assign_pointer(flow->sf_acts, acts); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1183 | |
| 1184 | if (unlikely(reply)) { |
| 1185 | error = ovs_flow_cmd_fill_info(flow, |
| 1186 | ovs_header->dp_ifindex, |
| 1187 | reply, info->snd_portid, |
| 1188 | info->snd_seq, 0, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 1189 | OVS_FLOW_CMD_SET, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1190 | ufid_flags); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1191 | BUG_ON(error < 0); |
| 1192 | } |
| 1193 | } else { |
| 1194 | /* Could not alloc without acts before locking. */ |
| 1195 | reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 1196 | info, OVS_FLOW_CMD_SET, false, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1197 | ufid_flags); |
| 1198 | |
Viresh Kumar | b5ffe63 | 2015-08-12 15:59:47 +0530 | [diff] [blame] | 1199 | if (IS_ERR(reply)) { |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1200 | error = PTR_ERR(reply); |
| 1201 | goto err_unlock_ovs; |
| 1202 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1203 | } |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1204 | |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1205 | /* Clear stats. */ |
| 1206 | if (a[OVS_FLOW_ATTR_CLEAR]) |
| 1207 | ovs_flow_stats_clear(flow); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1208 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1209 | |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1210 | if (reply) |
| 1211 | ovs_notify(&dp_flow_genl_family, reply, info); |
| 1212 | if (old_acts) |
Thomas Graf | 34ae932 | 2015-07-21 10:44:03 +0200 | [diff] [blame] | 1213 | ovs_nla_free_flow_actions_rcu(old_acts); |
Jarno Rajahalme | fb5d1e9 | 2014-05-05 13:13:14 -0700 | [diff] [blame] | 1214 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1215 | return 0; |
| 1216 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1217 | err_unlock_ovs: |
| 1218 | ovs_unlock(); |
Jarno Rajahalme | 893f139 | 2014-05-05 15:22:25 -0700 | [diff] [blame] | 1219 | kfree_skb(reply); |
| 1220 | err_kfree_acts: |
Thomas Graf | 34ae932 | 2015-07-21 10:44:03 +0200 | [diff] [blame] | 1221 | ovs_nla_free_flow_actions(acts); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1222 | error: |
| 1223 | return error; |
| 1224 | } |
| 1225 | |
| 1226 | static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) |
| 1227 | { |
| 1228 | struct nlattr **a = info->attrs; |
| 1229 | struct ovs_header *ovs_header = info->userhdr; |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 1230 | struct net *net = sock_net(skb->sk); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1231 | struct sw_flow_key key; |
| 1232 | struct sk_buff *reply; |
| 1233 | struct sw_flow *flow; |
| 1234 | struct datapath *dp; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 1235 | struct sw_flow_match match; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1236 | struct sw_flow_id ufid; |
| 1237 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); |
| 1238 | int err = 0; |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 1239 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1240 | bool ufid_present; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1241 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1242 | ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); |
| 1243 | if (a[OVS_FLOW_ATTR_KEY]) { |
pravin shelar | 2279994 | 2016-09-19 13:51:00 -0700 | [diff] [blame] | 1244 | ovs_match_init(&match, &key, true, NULL); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 1245 | err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1246 | log); |
| 1247 | } else if (!ufid_present) { |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 1248 | OVS_NLERR(log, |
| 1249 | "Flow get message rejected, Key attribute missing."); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1250 | err = -EINVAL; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 1251 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1252 | if (err) |
| 1253 | return err; |
| 1254 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1255 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1256 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1257 | if (!dp) { |
| 1258 | err = -ENODEV; |
| 1259 | goto unlock; |
| 1260 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1261 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1262 | if (ufid_present) |
| 1263 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); |
| 1264 | else |
| 1265 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); |
Alex Wang | 4a46b24 | 2014-06-30 20:30:29 -0700 | [diff] [blame] | 1266 | if (!flow) { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1267 | err = -ENOENT; |
| 1268 | goto unlock; |
| 1269 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1270 | |
Jarno Rajahalme | 0e9796b | 2014-05-05 14:28:07 -0700 | [diff] [blame] | 1271 | reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 1272 | OVS_FLOW_CMD_GET, true, ufid_flags); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1273 | if (IS_ERR(reply)) { |
| 1274 | err = PTR_ERR(reply); |
| 1275 | goto unlock; |
| 1276 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1277 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1278 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1279 | return genlmsg_reply(reply, info); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1280 | unlock: |
| 1281 | ovs_unlock(); |
| 1282 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) |
| 1286 | { |
| 1287 | struct nlattr **a = info->attrs; |
| 1288 | struct ovs_header *ovs_header = info->userhdr; |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 1289 | struct net *net = sock_net(skb->sk); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1290 | struct sw_flow_key key; |
| 1291 | struct sk_buff *reply; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1292 | struct sw_flow *flow = NULL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1293 | struct datapath *dp; |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 1294 | struct sw_flow_match match; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1295 | struct sw_flow_id ufid; |
| 1296 | u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1297 | int err; |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 1298 | bool log = !a[OVS_FLOW_ATTR_PROBE]; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1299 | bool ufid_present; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1300 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1301 | ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log); |
| 1302 | if (a[OVS_FLOW_ATTR_KEY]) { |
pravin shelar | 2279994 | 2016-09-19 13:51:00 -0700 | [diff] [blame] | 1303 | ovs_match_init(&match, &key, true, NULL); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 1304 | err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], |
| 1305 | NULL, log); |
Jarno Rajahalme | aed0677 | 2014-05-05 14:40:13 -0700 | [diff] [blame] | 1306 | if (unlikely(err)) |
| 1307 | return err; |
| 1308 | } |
| 1309 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1310 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1311 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Jarno Rajahalme | aed0677 | 2014-05-05 14:40:13 -0700 | [diff] [blame] | 1312 | if (unlikely(!dp)) { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1313 | err = -ENODEV; |
| 1314 | goto unlock; |
| 1315 | } |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1316 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1317 | if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) { |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 1318 | err = ovs_flow_tbl_flush(&dp->table); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1319 | goto unlock; |
| 1320 | } |
Andy Zhou | 03f0d91 | 2013-08-07 20:01:00 -0700 | [diff] [blame] | 1321 | |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1322 | if (ufid_present) |
| 1323 | flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid); |
| 1324 | else |
| 1325 | flow = ovs_flow_tbl_lookup_exact(&dp->table, &match); |
Alex Wang | 4a46b24 | 2014-06-30 20:30:29 -0700 | [diff] [blame] | 1326 | if (unlikely(!flow)) { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1327 | err = -ENOENT; |
| 1328 | goto unlock; |
| 1329 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1330 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 1331 | ovs_flow_tbl_remove(&dp->table, flow); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1332 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1333 | |
Jarno Rajahalme | aed0677 | 2014-05-05 14:40:13 -0700 | [diff] [blame] | 1334 | reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1335 | &flow->id, info, false, ufid_flags); |
Jarno Rajahalme | aed0677 | 2014-05-05 14:40:13 -0700 | [diff] [blame] | 1336 | if (likely(reply)) { |
Enrico Weigelt | b90f5aa | 2019-06-05 23:06:40 +0200 | [diff] [blame] | 1337 | if (!IS_ERR(reply)) { |
Jarno Rajahalme | aed0677 | 2014-05-05 14:40:13 -0700 | [diff] [blame] | 1338 | rcu_read_lock(); /*To keep RCU checker happy. */ |
| 1339 | err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, |
| 1340 | reply, info->snd_portid, |
| 1341 | info->snd_seq, 0, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1342 | OVS_FLOW_CMD_DEL, |
| 1343 | ufid_flags); |
Jarno Rajahalme | aed0677 | 2014-05-05 14:40:13 -0700 | [diff] [blame] | 1344 | rcu_read_unlock(); |
| 1345 | BUG_ON(err < 0); |
| 1346 | |
| 1347 | ovs_notify(&dp_flow_genl_family, reply, info); |
| 1348 | } else { |
| 1349 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply)); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | ovs_flow_free(flow, true); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1354 | return 0; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1355 | unlock: |
| 1356 | ovs_unlock(); |
| 1357 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1361 | { |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1362 | struct nlattr *a[__OVS_FLOW_ATTR_MAX]; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1363 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 1364 | struct table_instance *ti; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1365 | struct datapath *dp; |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1366 | u32 ufid_flags; |
| 1367 | int err; |
| 1368 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1369 | err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a, |
| 1370 | OVS_FLOW_ATTR_MAX, flow_policy, NULL); |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1371 | if (err) |
| 1372 | return err; |
| 1373 | ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1374 | |
Pravin B Shelar | d57170b | 2013-07-30 15:39:39 -0700 | [diff] [blame] | 1375 | rcu_read_lock(); |
Andy Zhou | cc3a5ae | 2014-09-08 13:14:22 -0700 | [diff] [blame] | 1376 | dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1377 | if (!dp) { |
Pravin B Shelar | d57170b | 2013-07-30 15:39:39 -0700 | [diff] [blame] | 1378 | rcu_read_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1379 | return -ENODEV; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1380 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1381 | |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 1382 | ti = rcu_dereference(dp->table.ti); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1383 | for (;;) { |
| 1384 | struct sw_flow *flow; |
| 1385 | u32 bucket, obj; |
| 1386 | |
| 1387 | bucket = cb->args[0]; |
| 1388 | obj = cb->args[1]; |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 1389 | flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1390 | if (!flow) |
| 1391 | break; |
| 1392 | |
Jarno Rajahalme | 0e9796b | 2014-05-05 14:28:07 -0700 | [diff] [blame] | 1393 | 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] | 1394 | NETLINK_CB(cb->skb).portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1395 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 1396 | OVS_FLOW_CMD_GET, ufid_flags) < 0) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1397 | break; |
| 1398 | |
| 1399 | cb->args[0] = bucket; |
| 1400 | cb->args[1] = obj; |
| 1401 | } |
Pravin B Shelar | d57170b | 2013-07-30 15:39:39 -0700 | [diff] [blame] | 1402 | rcu_read_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1403 | return skb->len; |
| 1404 | } |
| 1405 | |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 1406 | static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { |
| 1407 | [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 1408 | [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED }, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 1409 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, |
| 1410 | [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, |
Jarno Rajahalme | 05da589 | 2014-11-06 07:03:05 -0800 | [diff] [blame] | 1411 | [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG }, |
Joe Stringer | 74ed7ab | 2015-01-21 16:42:52 -0800 | [diff] [blame] | 1412 | [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 }, |
| 1413 | [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 }, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 1414 | }; |
| 1415 | |
stephen hemminger | 48e48a7 | 2014-07-16 11:25:52 -0700 | [diff] [blame] | 1416 | static const struct genl_ops dp_flow_genl_ops[] = { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1417 | { .cmd = OVS_FLOW_CMD_NEW, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1418 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 1419 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1420 | .doit = ovs_flow_cmd_new |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1421 | }, |
| 1422 | { .cmd = OVS_FLOW_CMD_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1423 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 1424 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1425 | .doit = ovs_flow_cmd_del |
| 1426 | }, |
| 1427 | { .cmd = OVS_FLOW_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1428 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1429 | .flags = 0, /* OK for unprivileged users. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1430 | .doit = ovs_flow_cmd_get, |
| 1431 | .dumpit = ovs_flow_cmd_dump |
| 1432 | }, |
| 1433 | { .cmd = OVS_FLOW_CMD_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1434 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 1435 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jarno Rajahalme | 37bdc87 | 2014-05-05 14:53:51 -0700 | [diff] [blame] | 1436 | .doit = ovs_flow_cmd_set, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1437 | }, |
| 1438 | }; |
| 1439 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1440 | static struct genl_family dp_flow_genl_family __ro_after_init = { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1441 | .hdrsize = sizeof(struct ovs_header), |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 1442 | .name = OVS_FLOW_FAMILY, |
| 1443 | .version = OVS_FLOW_VERSION, |
| 1444 | .maxattr = OVS_FLOW_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 1445 | .policy = flow_policy, |
Pravin B Shelar | 3a4e0d6 | 2013-04-23 07:48:48 +0000 | [diff] [blame] | 1446 | .netnsok = true, |
| 1447 | .parallel_ops = true, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 1448 | .ops = dp_flow_genl_ops, |
| 1449 | .n_ops = ARRAY_SIZE(dp_flow_genl_ops), |
| 1450 | .mcgrps = &ovs_dp_flow_multicast_group, |
| 1451 | .n_mcgrps = 1, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1452 | .module = THIS_MODULE, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1453 | }; |
| 1454 | |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 1455 | static size_t ovs_dp_cmd_msg_size(void) |
| 1456 | { |
| 1457 | size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); |
| 1458 | |
| 1459 | msgsize += nla_total_size(IFNAMSIZ); |
Nicolas Dichtel | 66c7a5e | 2016-04-26 10:06:15 +0200 | [diff] [blame] | 1460 | msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats)); |
| 1461 | msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats)); |
Daniele Di Proietto | 45fb9c3 | 2014-01-23 10:47:35 -0800 | [diff] [blame] | 1462 | msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */ |
Thomas Graf | c3ff8cf | 2013-03-29 14:46:49 +0100 | [diff] [blame] | 1463 | |
| 1464 | return msgsize; |
| 1465 | } |
| 1466 | |
Pravin B Shelar | 8ec609d | 2014-11-11 15:55:16 -0800 | [diff] [blame] | 1467 | /* Called with ovs_mutex. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1468 | 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] | 1469 | u32 portid, u32 seq, u32 flags, u8 cmd) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1470 | { |
| 1471 | struct ovs_header *ovs_header; |
| 1472 | struct ovs_dp_stats dp_stats; |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 1473 | struct ovs_dp_megaflow_stats dp_megaflow_stats; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1474 | int err; |
| 1475 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1476 | ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1477 | flags, cmd); |
| 1478 | if (!ovs_header) |
| 1479 | goto error; |
| 1480 | |
| 1481 | ovs_header->dp_ifindex = get_dpifindex(dp); |
| 1482 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1483 | 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] | 1484 | if (err) |
| 1485 | goto nla_put_failure; |
| 1486 | |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 1487 | get_dp_stats(dp, &dp_stats, &dp_megaflow_stats); |
Nicolas Dichtel | 66c7a5e | 2016-04-26 10:06:15 +0200 | [diff] [blame] | 1488 | if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), |
| 1489 | &dp_stats, OVS_DP_ATTR_PAD)) |
Andy Zhou | 1bd7116 | 2013-10-22 10:42:46 -0700 | [diff] [blame] | 1490 | goto nla_put_failure; |
| 1491 | |
Nicolas Dichtel | 66c7a5e | 2016-04-26 10:06:15 +0200 | [diff] [blame] | 1492 | if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS, |
| 1493 | sizeof(struct ovs_dp_megaflow_stats), |
| 1494 | &dp_megaflow_stats, OVS_DP_ATTR_PAD)) |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1495 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1496 | |
Thomas Graf | 43d4be9 | 2013-12-13 15:22:18 +0100 | [diff] [blame] | 1497 | if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features)) |
| 1498 | goto nla_put_failure; |
| 1499 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1500 | genlmsg_end(skb, ovs_header); |
| 1501 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1502 | |
| 1503 | nla_put_failure: |
| 1504 | genlmsg_cancel(skb, ovs_header); |
| 1505 | error: |
| 1506 | return -EMSGSIZE; |
| 1507 | } |
| 1508 | |
Florian Westphal | 263ea09 | 2016-02-18 15:03:26 +0100 | [diff] [blame] | 1509 | static struct sk_buff *ovs_dp_cmd_alloc_info(void) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1510 | { |
Florian Westphal | 551ddc0 | 2016-02-18 15:03:25 +0100 | [diff] [blame] | 1511 | return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Jarno Rajahalme | bb6f9a7 | 2014-05-05 11:32:17 -0700 | [diff] [blame] | 1514 | /* Called with rcu_read_lock or ovs_mutex. */ |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1515 | static struct datapath *lookup_datapath(struct net *net, |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 1516 | const struct ovs_header *ovs_header, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1517 | struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
| 1518 | { |
| 1519 | struct datapath *dp; |
| 1520 | |
| 1521 | if (!a[OVS_DP_ATTR_NAME]) |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1522 | dp = get_dp(net, ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1523 | else { |
| 1524 | struct vport *vport; |
| 1525 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1526 | vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1527 | dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1528 | } |
| 1529 | return dp ? dp : ERR_PTR(-ENODEV); |
| 1530 | } |
| 1531 | |
Thomas Graf | 44da5ae | 2013-12-13 15:22:19 +0100 | [diff] [blame] | 1532 | static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info) |
| 1533 | { |
| 1534 | struct datapath *dp; |
| 1535 | |
| 1536 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
Jiri Pirko | 3c7eacf | 2014-02-14 11:42:36 +0100 | [diff] [blame] | 1537 | if (IS_ERR(dp)) |
Thomas Graf | 44da5ae | 2013-12-13 15:22:19 +0100 | [diff] [blame] | 1538 | return; |
| 1539 | |
| 1540 | WARN(dp->user_features, "Dropping previously announced user features\n"); |
| 1541 | dp->user_features = 0; |
| 1542 | } |
| 1543 | |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 1544 | static void ovs_dp_change(struct datapath *dp, struct nlattr *a[]) |
Thomas Graf | 43d4be9 | 2013-12-13 15:22:18 +0100 | [diff] [blame] | 1545 | { |
| 1546 | if (a[OVS_DP_ATTR_USER_FEATURES]) |
| 1547 | dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]); |
| 1548 | } |
| 1549 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1550 | static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) |
| 1551 | { |
| 1552 | struct nlattr **a = info->attrs; |
| 1553 | struct vport_parms parms; |
| 1554 | struct sk_buff *reply; |
| 1555 | struct datapath *dp; |
| 1556 | struct vport *vport; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1557 | struct ovs_net *ovs_net; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1558 | int err, i; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1559 | |
| 1560 | err = -EINVAL; |
| 1561 | if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) |
| 1562 | goto err; |
| 1563 | |
Florian Westphal | 263ea09 | 2016-02-18 15:03:26 +0100 | [diff] [blame] | 1564 | reply = ovs_dp_cmd_alloc_info(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1565 | if (!reply) |
| 1566 | return -ENOMEM; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1567 | |
| 1568 | err = -ENOMEM; |
| 1569 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); |
| 1570 | if (dp == NULL) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1571 | goto err_free_reply; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1572 | |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 1573 | ovs_dp_set_net(dp, sock_net(skb->sk)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1574 | |
| 1575 | /* Allocate table. */ |
Pravin B Shelar | b637e49 | 2013-10-04 00:14:23 -0700 | [diff] [blame] | 1576 | err = ovs_flow_tbl_init(&dp->table); |
| 1577 | if (err) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1578 | goto err_free_dp; |
| 1579 | |
WANG Cong | 1c213bd | 2014-02-13 11:46:28 -0800 | [diff] [blame] | 1580 | dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1581 | if (!dp->stats_percpu) { |
| 1582 | err = -ENOMEM; |
| 1583 | goto err_destroy_table; |
| 1584 | } |
| 1585 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1586 | dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS, |
| 1587 | sizeof(struct hlist_head), |
| 1588 | GFP_KERNEL); |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1589 | if (!dp->ports) { |
| 1590 | err = -ENOMEM; |
| 1591 | goto err_destroy_percpu; |
| 1592 | } |
| 1593 | |
| 1594 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) |
| 1595 | INIT_HLIST_HEAD(&dp->ports[i]); |
| 1596 | |
Andy Zhou | 96fbc13 | 2017-11-10 12:09:42 -0800 | [diff] [blame] | 1597 | err = ovs_meters_init(dp); |
| 1598 | if (err) |
| 1599 | goto err_destroy_ports_array; |
| 1600 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1601 | /* Set up our datapath device. */ |
| 1602 | parms.name = nla_data(a[OVS_DP_ATTR_NAME]); |
| 1603 | parms.type = OVS_VPORT_TYPE_INTERNAL; |
| 1604 | parms.options = NULL; |
| 1605 | parms.dp = dp; |
| 1606 | parms.port_no = OVSP_LOCAL; |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 1607 | parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID]; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1608 | |
Thomas Graf | 43d4be9 | 2013-12-13 15:22:18 +0100 | [diff] [blame] | 1609 | ovs_dp_change(dp, a); |
| 1610 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1611 | /* So far only local changes have been made, now need the lock. */ |
| 1612 | ovs_lock(); |
| 1613 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1614 | vport = new_vport(&parms); |
| 1615 | if (IS_ERR(vport)) { |
| 1616 | err = PTR_ERR(vport); |
| 1617 | if (err == -EBUSY) |
| 1618 | err = -EEXIST; |
| 1619 | |
Thomas Graf | 44da5ae | 2013-12-13 15:22:19 +0100 | [diff] [blame] | 1620 | if (err == -EEXIST) { |
| 1621 | /* An outdated user space instance that does not understand |
| 1622 | * the concept of user_features has attempted to create a new |
| 1623 | * datapath and is likely to reuse it. Drop all user features. |
| 1624 | */ |
| 1625 | if (info->genlhdr->version < OVS_DP_VER_FEATURES) |
| 1626 | ovs_dp_reset_user_features(skb, info); |
| 1627 | } |
| 1628 | |
Andy Zhou | 96fbc13 | 2017-11-10 12:09:42 -0800 | [diff] [blame] | 1629 | goto err_destroy_meters; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1632 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
| 1633 | info->snd_seq, 0, OVS_DP_CMD_NEW); |
| 1634 | BUG_ON(err < 0); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1635 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1636 | 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] | 1637 | list_add_tail_rcu(&dp->list_node, &ovs_net->dps); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1638 | |
| 1639 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1640 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1641 | ovs_notify(&dp_datapath_genl_family, reply, info); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1642 | return 0; |
| 1643 | |
Andy Zhou | 96fbc13 | 2017-11-10 12:09:42 -0800 | [diff] [blame] | 1644 | err_destroy_meters: |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1645 | ovs_unlock(); |
Andy Zhou | 96fbc13 | 2017-11-10 12:09:42 -0800 | [diff] [blame] | 1646 | ovs_meters_exit(dp); |
| 1647 | err_destroy_ports_array: |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1648 | kfree(dp->ports); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1649 | err_destroy_percpu: |
| 1650 | free_percpu(dp->stats_percpu); |
| 1651 | err_destroy_table: |
Pravin B Shelar | 9b996e5 | 2014-05-06 18:41:20 -0700 | [diff] [blame] | 1652 | ovs_flow_tbl_destroy(&dp->table); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1653 | err_free_dp: |
| 1654 | kfree(dp); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1655 | err_free_reply: |
| 1656 | kfree_skb(reply); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1657 | err: |
| 1658 | return err; |
| 1659 | } |
| 1660 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1661 | /* Called with ovs_mutex. */ |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1662 | static void __dp_destroy(struct datapath *dp) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1663 | { |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1664 | int i; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1665 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1666 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
| 1667 | struct vport *vport; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1668 | struct hlist_node *n; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 1669 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1670 | 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] | 1671 | if (vport->port_no != OVSP_LOCAL) |
| 1672 | ovs_dp_detach_port(vport); |
| 1673 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1674 | |
Pravin B Shelar | 59a35d6 | 2013-07-30 15:42:19 -0700 | [diff] [blame] | 1675 | list_del_rcu(&dp->list_node); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1676 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1677 | /* OVSP_LOCAL is datapath internal port. We need to make sure that |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 1678 | * all ports in datapath are destroyed first before freeing datapath. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1679 | */ |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1680 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1681 | |
Andy Zhou | e80857c | 2014-01-21 09:31:04 -0800 | [diff] [blame] | 1682 | /* RCU destroy the flow table */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1683 | call_rcu(&dp->rcu, destroy_dp_rcu); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) |
| 1687 | { |
| 1688 | struct sk_buff *reply; |
| 1689 | struct datapath *dp; |
| 1690 | int err; |
| 1691 | |
Florian Westphal | 263ea09 | 2016-02-18 15:03:26 +0100 | [diff] [blame] | 1692 | reply = ovs_dp_cmd_alloc_info(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1693 | if (!reply) |
| 1694 | return -ENOMEM; |
| 1695 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1696 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1697 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
| 1698 | err = PTR_ERR(dp); |
| 1699 | if (IS_ERR(dp)) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1700 | goto err_unlock_free; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1701 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1702 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
| 1703 | info->snd_seq, 0, OVS_DP_CMD_DEL); |
| 1704 | BUG_ON(err < 0); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1705 | |
| 1706 | __dp_destroy(dp); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1707 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1708 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1709 | ovs_notify(&dp_datapath_genl_family, reply, info); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1710 | |
| 1711 | return 0; |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1712 | |
| 1713 | err_unlock_free: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1714 | ovs_unlock(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1715 | kfree_skb(reply); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1716 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) |
| 1720 | { |
| 1721 | struct sk_buff *reply; |
| 1722 | struct datapath *dp; |
| 1723 | int err; |
| 1724 | |
Florian Westphal | 263ea09 | 2016-02-18 15:03:26 +0100 | [diff] [blame] | 1725 | reply = ovs_dp_cmd_alloc_info(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1726 | if (!reply) |
| 1727 | return -ENOMEM; |
| 1728 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1729 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1730 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1731 | err = PTR_ERR(dp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1732 | if (IS_ERR(dp)) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1733 | goto err_unlock_free; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1734 | |
Thomas Graf | 43d4be9 | 2013-12-13 15:22:18 +0100 | [diff] [blame] | 1735 | ovs_dp_change(dp, info->attrs); |
| 1736 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1737 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 1738 | info->snd_seq, 0, OVS_DP_CMD_SET); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1739 | BUG_ON(err < 0); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1740 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1741 | ovs_unlock(); |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1742 | ovs_notify(&dp_datapath_genl_family, reply, info); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1743 | |
| 1744 | return 0; |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1745 | |
| 1746 | err_unlock_free: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1747 | ovs_unlock(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1748 | kfree_skb(reply); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1749 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) |
| 1753 | { |
| 1754 | struct sk_buff *reply; |
| 1755 | struct datapath *dp; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1756 | int err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1757 | |
Florian Westphal | 263ea09 | 2016-02-18 15:03:26 +0100 | [diff] [blame] | 1758 | reply = ovs_dp_cmd_alloc_info(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1759 | if (!reply) |
| 1760 | return -ENOMEM; |
| 1761 | |
Pravin B Shelar | 8ec609d | 2014-11-11 15:55:16 -0800 | [diff] [blame] | 1762 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1763 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1764 | if (IS_ERR(dp)) { |
| 1765 | err = PTR_ERR(dp); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1766 | goto err_unlock_free; |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1767 | } |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1768 | err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 1769 | info->snd_seq, 0, OVS_DP_CMD_GET); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1770 | BUG_ON(err < 0); |
Pravin B Shelar | 8ec609d | 2014-11-11 15:55:16 -0800 | [diff] [blame] | 1771 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1772 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1773 | return genlmsg_reply(reply, info); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1774 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1775 | err_unlock_free: |
Pravin B Shelar | 8ec609d | 2014-11-11 15:55:16 -0800 | [diff] [blame] | 1776 | ovs_unlock(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1777 | kfree_skb(reply); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1778 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 1782 | { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1783 | 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] | 1784 | struct datapath *dp; |
| 1785 | int skip = cb->args[0]; |
| 1786 | int i = 0; |
| 1787 | |
Pravin B Shelar | 8ec609d | 2014-11-11 15:55:16 -0800 | [diff] [blame] | 1788 | ovs_lock(); |
| 1789 | list_for_each_entry(dp, &ovs_net->dps, list_node) { |
Ben Pfaff | 77676fd | 2012-01-17 13:33:39 +0000 | [diff] [blame] | 1790 | if (i >= skip && |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1791 | ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1792 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 1793 | OVS_DP_CMD_GET) < 0) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1794 | break; |
| 1795 | i++; |
| 1796 | } |
Pravin B Shelar | 8ec609d | 2014-11-11 15:55:16 -0800 | [diff] [blame] | 1797 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1798 | |
| 1799 | cb->args[0] = i; |
| 1800 | |
| 1801 | return skb->len; |
| 1802 | } |
| 1803 | |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 1804 | static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { |
| 1805 | [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, |
| 1806 | [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, |
| 1807 | [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 }, |
| 1808 | }; |
| 1809 | |
stephen hemminger | 48e48a7 | 2014-07-16 11:25:52 -0700 | [diff] [blame] | 1810 | static const struct genl_ops dp_datapath_genl_ops[] = { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1811 | { .cmd = OVS_DP_CMD_NEW, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1812 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 1813 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1814 | .doit = ovs_dp_cmd_new |
| 1815 | }, |
| 1816 | { .cmd = OVS_DP_CMD_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1817 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 1818 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1819 | .doit = ovs_dp_cmd_del |
| 1820 | }, |
| 1821 | { .cmd = OVS_DP_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1822 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1823 | .flags = 0, /* OK for unprivileged users. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1824 | .doit = ovs_dp_cmd_get, |
| 1825 | .dumpit = ovs_dp_cmd_dump |
| 1826 | }, |
| 1827 | { .cmd = OVS_DP_CMD_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1828 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 1829 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1830 | .doit = ovs_dp_cmd_set, |
| 1831 | }, |
| 1832 | }; |
| 1833 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1834 | static struct genl_family dp_datapath_genl_family __ro_after_init = { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1835 | .hdrsize = sizeof(struct ovs_header), |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 1836 | .name = OVS_DATAPATH_FAMILY, |
| 1837 | .version = OVS_DATAPATH_VERSION, |
| 1838 | .maxattr = OVS_DP_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 1839 | .policy = datapath_policy, |
Pravin B Shelar | 3a4e0d6 | 2013-04-23 07:48:48 +0000 | [diff] [blame] | 1840 | .netnsok = true, |
| 1841 | .parallel_ops = true, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 1842 | .ops = dp_datapath_genl_ops, |
| 1843 | .n_ops = ARRAY_SIZE(dp_datapath_genl_ops), |
| 1844 | .mcgrps = &ovs_dp_datapath_multicast_group, |
| 1845 | .n_mcgrps = 1, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1846 | .module = THIS_MODULE, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1847 | }; |
| 1848 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1849 | /* Called with ovs_mutex or RCU read lock. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1850 | static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 1851 | struct net *net, u32 portid, u32 seq, |
| 1852 | u32 flags, u8 cmd) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1853 | { |
| 1854 | struct ovs_header *ovs_header; |
| 1855 | struct ovs_vport_stats vport_stats; |
| 1856 | int err; |
| 1857 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1858 | ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1859 | flags, cmd); |
| 1860 | if (!ovs_header) |
| 1861 | return -EMSGSIZE; |
| 1862 | |
| 1863 | ovs_header->dp_ifindex = get_dpifindex(vport->dp); |
| 1864 | |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1865 | if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || |
| 1866 | nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 1867 | nla_put_string(skb, OVS_VPORT_ATTR_NAME, |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 1868 | ovs_vport_name(vport)) || |
| 1869 | nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex)) |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1870 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1871 | |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 1872 | if (!net_eq(net, dev_net(vport->dev))) { |
| 1873 | int id = peernet2id_alloc(net, dev_net(vport->dev)); |
| 1874 | |
| 1875 | if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id)) |
| 1876 | goto nla_put_failure; |
| 1877 | } |
| 1878 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1879 | ovs_vport_get_stats(vport, &vport_stats); |
Nicolas Dichtel | 66c7a5e | 2016-04-26 10:06:15 +0200 | [diff] [blame] | 1880 | if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS, |
| 1881 | sizeof(struct ovs_vport_stats), &vport_stats, |
| 1882 | OVS_VPORT_ATTR_PAD)) |
David S. Miller | 028d6a6 | 2012-03-29 23:20:48 -0400 | [diff] [blame] | 1883 | goto nla_put_failure; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1884 | |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 1885 | if (ovs_vport_get_upcall_portids(vport, skb)) |
| 1886 | goto nla_put_failure; |
| 1887 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1888 | err = ovs_vport_get_options(vport, skb); |
| 1889 | if (err == -EMSGSIZE) |
| 1890 | goto error; |
| 1891 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1892 | genlmsg_end(skb, ovs_header); |
| 1893 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1894 | |
| 1895 | nla_put_failure: |
| 1896 | err = -EMSGSIZE; |
| 1897 | error: |
| 1898 | genlmsg_cancel(skb, ovs_header); |
| 1899 | return err; |
| 1900 | } |
| 1901 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 1902 | static struct sk_buff *ovs_vport_cmd_alloc_info(void) |
| 1903 | { |
| 1904 | return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 1905 | } |
| 1906 | |
| 1907 | /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */ |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 1908 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net, |
| 1909 | u32 portid, u32 seq, u8 cmd) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1910 | { |
| 1911 | struct sk_buff *skb; |
| 1912 | int retval; |
| 1913 | |
| 1914 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
| 1915 | if (!skb) |
| 1916 | return ERR_PTR(-ENOMEM); |
| 1917 | |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 1918 | retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd); |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 1919 | BUG_ON(retval < 0); |
| 1920 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1921 | return skb; |
| 1922 | } |
| 1923 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1924 | /* Called with ovs_mutex or RCU read lock. */ |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1925 | static struct vport *lookup_vport(struct net *net, |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 1926 | const struct ovs_header *ovs_header, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1927 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
| 1928 | { |
| 1929 | struct datapath *dp; |
| 1930 | struct vport *vport; |
| 1931 | |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 1932 | if (a[OVS_VPORT_ATTR_IFINDEX]) |
| 1933 | return ERR_PTR(-EOPNOTSUPP); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1934 | if (a[OVS_VPORT_ATTR_NAME]) { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1935 | vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1936 | if (!vport) |
| 1937 | return ERR_PTR(-ENODEV); |
Ben Pfaff | 651a68e | 2012-03-06 15:04:04 -0800 | [diff] [blame] | 1938 | if (ovs_header->dp_ifindex && |
| 1939 | ovs_header->dp_ifindex != get_dpifindex(vport->dp)) |
| 1940 | return ERR_PTR(-ENODEV); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1941 | return vport; |
| 1942 | } else if (a[OVS_VPORT_ATTR_PORT_NO]) { |
| 1943 | u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); |
| 1944 | |
| 1945 | if (port_no >= DP_MAX_PORTS) |
| 1946 | return ERR_PTR(-EFBIG); |
| 1947 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 1948 | dp = get_dp(net, ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1949 | if (!dp) |
| 1950 | return ERR_PTR(-ENODEV); |
| 1951 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 1952 | vport = ovs_vport_ovsl_rcu(dp, port_no); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1953 | if (!vport) |
Jarno Rajahalme | 14408db | 2013-01-09 14:27:35 -0800 | [diff] [blame] | 1954 | return ERR_PTR(-ENODEV); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1955 | return vport; |
| 1956 | } else |
| 1957 | return ERR_PTR(-EINVAL); |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 1958 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 1961 | static unsigned int ovs_get_max_headroom(struct datapath *dp) |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 1962 | { |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 1963 | unsigned int dev_headroom, max_headroom = 0; |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 1964 | struct net_device *dev; |
| 1965 | struct vport *vport; |
| 1966 | int i; |
| 1967 | |
| 1968 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
| 1969 | hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { |
| 1970 | dev = vport->dev; |
| 1971 | dev_headroom = netdev_get_fwd_headroom(dev); |
| 1972 | if (dev_headroom > max_headroom) |
| 1973 | max_headroom = dev_headroom; |
| 1974 | } |
| 1975 | } |
| 1976 | |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 1977 | return max_headroom; |
| 1978 | } |
| 1979 | |
| 1980 | /* Called with ovs_mutex */ |
| 1981 | static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom) |
| 1982 | { |
| 1983 | struct vport *vport; |
| 1984 | int i; |
| 1985 | |
| 1986 | dp->max_headroom = new_headroom; |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 1987 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) |
| 1988 | hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 1989 | netdev_set_rx_headroom(vport->dev, new_headroom); |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 1990 | } |
| 1991 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1992 | static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) |
| 1993 | { |
| 1994 | struct nlattr **a = info->attrs; |
| 1995 | struct ovs_header *ovs_header = info->userhdr; |
| 1996 | struct vport_parms parms; |
| 1997 | struct sk_buff *reply; |
| 1998 | struct vport *vport; |
| 1999 | struct datapath *dp; |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 2000 | unsigned int new_headroom; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2001 | u32 port_no; |
| 2002 | int err; |
| 2003 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2004 | if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || |
| 2005 | !a[OVS_VPORT_ATTR_UPCALL_PID]) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2006 | return -EINVAL; |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 2007 | if (a[OVS_VPORT_ATTR_IFINDEX]) |
| 2008 | return -EOPNOTSUPP; |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2009 | |
| 2010 | port_no = a[OVS_VPORT_ATTR_PORT_NO] |
| 2011 | ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0; |
| 2012 | if (port_no >= DP_MAX_PORTS) |
| 2013 | return -EFBIG; |
| 2014 | |
| 2015 | reply = ovs_vport_cmd_alloc_info(); |
| 2016 | if (!reply) |
| 2017 | return -ENOMEM; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2018 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2019 | ovs_lock(); |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 2020 | restart: |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2021 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2022 | err = -ENODEV; |
| 2023 | if (!dp) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2024 | goto exit_unlock_free; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2025 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2026 | if (port_no) { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2027 | vport = ovs_vport_ovsl(dp, port_no); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2028 | err = -EBUSY; |
| 2029 | if (vport) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2030 | goto exit_unlock_free; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2031 | } else { |
| 2032 | for (port_no = 1; ; port_no++) { |
| 2033 | if (port_no >= DP_MAX_PORTS) { |
| 2034 | err = -EFBIG; |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2035 | goto exit_unlock_free; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2036 | } |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2037 | vport = ovs_vport_ovsl(dp, port_no); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2038 | if (!vport) |
| 2039 | break; |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); |
| 2044 | parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); |
| 2045 | parms.options = a[OVS_VPORT_ATTR_OPTIONS]; |
| 2046 | parms.dp = dp; |
| 2047 | parms.port_no = port_no; |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 2048 | parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID]; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2049 | |
| 2050 | vport = new_vport(&parms); |
| 2051 | err = PTR_ERR(vport); |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 2052 | if (IS_ERR(vport)) { |
| 2053 | if (err == -EAGAIN) |
| 2054 | goto restart; |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2055 | goto exit_unlock_free; |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 2056 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2057 | |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 2058 | err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), |
| 2059 | info->snd_portid, info->snd_seq, 0, |
| 2060 | OVS_VPORT_CMD_NEW); |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 2061 | |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 2062 | new_headroom = netdev_get_fwd_headroom(vport->dev); |
| 2063 | |
| 2064 | if (new_headroom > dp->max_headroom) |
| 2065 | ovs_update_headroom(dp, new_headroom); |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 2066 | else |
| 2067 | netdev_set_rx_headroom(vport->dev, dp->max_headroom); |
| 2068 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2069 | BUG_ON(err < 0); |
| 2070 | ovs_unlock(); |
Thomas Graf | ed66118 | 2013-03-29 14:46:50 +0100 | [diff] [blame] | 2071 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 2072 | ovs_notify(&dp_vport_genl_family, reply, info); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2073 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2074 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2075 | exit_unlock_free: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2076 | ovs_unlock(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2077 | kfree_skb(reply); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2078 | return err; |
| 2079 | } |
| 2080 | |
| 2081 | static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) |
| 2082 | { |
| 2083 | struct nlattr **a = info->attrs; |
| 2084 | struct sk_buff *reply; |
| 2085 | struct vport *vport; |
| 2086 | int err; |
| 2087 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2088 | reply = ovs_vport_cmd_alloc_info(); |
| 2089 | if (!reply) |
| 2090 | return -ENOMEM; |
| 2091 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2092 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2093 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2094 | err = PTR_ERR(vport); |
| 2095 | if (IS_ERR(vport)) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2096 | goto exit_unlock_free; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2097 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2098 | if (a[OVS_VPORT_ATTR_TYPE] && |
Jesse Gross | f44f340 | 2013-05-13 08:15:26 -0700 | [diff] [blame] | 2099 | nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2100 | err = -EINVAL; |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2101 | goto exit_unlock_free; |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
Jesse Gross | f44f340 | 2013-05-13 08:15:26 -0700 | [diff] [blame] | 2104 | if (a[OVS_VPORT_ATTR_OPTIONS]) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2105 | err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); |
Jesse Gross | f44f340 | 2013-05-13 08:15:26 -0700 | [diff] [blame] | 2106 | if (err) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2107 | goto exit_unlock_free; |
Jesse Gross | f44f340 | 2013-05-13 08:15:26 -0700 | [diff] [blame] | 2108 | } |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 2109 | |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 2110 | |
| 2111 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) { |
| 2112 | struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID]; |
| 2113 | |
| 2114 | err = ovs_vport_set_upcall_portids(vport, ids); |
| 2115 | if (err) |
| 2116 | goto exit_unlock_free; |
| 2117 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2118 | |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 2119 | err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), |
| 2120 | info->snd_portid, info->snd_seq, 0, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 2121 | OVS_VPORT_CMD_SET); |
Jesse Gross | a934151 | 2013-03-26 15:48:38 -0700 | [diff] [blame] | 2122 | BUG_ON(err < 0); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2123 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2124 | ovs_unlock(); |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 2125 | ovs_notify(&dp_vport_genl_family, reply, info); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2126 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2127 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2128 | exit_unlock_free: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2129 | ovs_unlock(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2130 | kfree_skb(reply); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2131 | return err; |
| 2132 | } |
| 2133 | |
| 2134 | static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) |
| 2135 | { |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 2136 | bool update_headroom = false; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2137 | struct nlattr **a = info->attrs; |
| 2138 | struct sk_buff *reply; |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 2139 | struct datapath *dp; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2140 | struct vport *vport; |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 2141 | unsigned int new_headroom; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2142 | int err; |
| 2143 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2144 | reply = ovs_vport_cmd_alloc_info(); |
| 2145 | if (!reply) |
| 2146 | return -ENOMEM; |
| 2147 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2148 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2149 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2150 | err = PTR_ERR(vport); |
| 2151 | if (IS_ERR(vport)) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2152 | goto exit_unlock_free; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2153 | |
| 2154 | if (vport->port_no == OVSP_LOCAL) { |
| 2155 | err = -EINVAL; |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2156 | goto exit_unlock_free; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 2159 | err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), |
| 2160 | info->snd_portid, info->snd_seq, 0, |
| 2161 | OVS_VPORT_CMD_DEL); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2162 | BUG_ON(err < 0); |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 2163 | |
| 2164 | /* the vport deletion may trigger dp headroom update */ |
| 2165 | dp = vport->dp; |
| 2166 | if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom) |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 2167 | update_headroom = true; |
| 2168 | |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 2169 | netdev_reset_rx_headroom(vport->dev); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2170 | ovs_dp_detach_port(vport); |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 2171 | |
Taehee Yoo | 6b660c4 | 2019-07-06 01:08:09 +0900 | [diff] [blame^] | 2172 | if (update_headroom) { |
| 2173 | new_headroom = ovs_get_max_headroom(dp); |
| 2174 | |
| 2175 | if (new_headroom < dp->max_headroom) |
| 2176 | ovs_update_headroom(dp, new_headroom); |
| 2177 | } |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2178 | ovs_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2179 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 2180 | ovs_notify(&dp_vport_genl_family, reply, info); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2181 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2182 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2183 | exit_unlock_free: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2184 | ovs_unlock(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2185 | kfree_skb(reply); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2186 | return err; |
| 2187 | } |
| 2188 | |
| 2189 | static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) |
| 2190 | { |
| 2191 | struct nlattr **a = info->attrs; |
| 2192 | struct ovs_header *ovs_header = info->userhdr; |
| 2193 | struct sk_buff *reply; |
| 2194 | struct vport *vport; |
| 2195 | int err; |
| 2196 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2197 | reply = ovs_vport_cmd_alloc_info(); |
| 2198 | if (!reply) |
| 2199 | return -ENOMEM; |
| 2200 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2201 | rcu_read_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2202 | vport = lookup_vport(sock_net(skb->sk), ovs_header, a); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2203 | err = PTR_ERR(vport); |
| 2204 | if (IS_ERR(vport)) |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2205 | goto exit_unlock_free; |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 2206 | err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info), |
| 2207 | info->snd_portid, info->snd_seq, 0, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 2208 | OVS_VPORT_CMD_GET); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2209 | BUG_ON(err < 0); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2210 | rcu_read_unlock(); |
| 2211 | |
| 2212 | return genlmsg_reply(reply, info); |
| 2213 | |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2214 | exit_unlock_free: |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2215 | rcu_read_unlock(); |
Jarno Rajahalme | 6093ae9 | 2014-05-05 14:13:32 -0700 | [diff] [blame] | 2216 | kfree_skb(reply); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2217 | return err; |
| 2218 | } |
| 2219 | |
| 2220 | static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 2221 | { |
| 2222 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); |
| 2223 | struct datapath *dp; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2224 | int bucket = cb->args[0], skip = cb->args[1]; |
| 2225 | int i, j = 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2226 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2227 | rcu_read_lock(); |
Andy Zhou | cc3a5ae | 2014-09-08 13:14:22 -0700 | [diff] [blame] | 2228 | dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex); |
Jarno Rajahalme | 42ee19e | 2014-02-15 17:42:29 -0800 | [diff] [blame] | 2229 | if (!dp) { |
| 2230 | rcu_read_unlock(); |
| 2231 | return -ENODEV; |
| 2232 | } |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2233 | for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2234 | struct vport *vport; |
| 2235 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2236 | j = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2237 | 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] | 2238 | if (j >= skip && |
| 2239 | ovs_vport_cmd_fill_info(vport, skb, |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 2240 | sock_net(skb->sk), |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2241 | NETLINK_CB(cb->skb).portid, |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2242 | cb->nlh->nlmsg_seq, |
| 2243 | NLM_F_MULTI, |
Yifeng Sun | 804fe10 | 2018-09-26 11:40:14 -0700 | [diff] [blame] | 2244 | OVS_VPORT_CMD_GET) < 0) |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2245 | goto out; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2246 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2247 | j++; |
| 2248 | } |
| 2249 | skip = 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2250 | } |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2251 | out: |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2252 | rcu_read_unlock(); |
| 2253 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2254 | cb->args[0] = i; |
| 2255 | cb->args[1] = j; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2256 | |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 2257 | return skb->len; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 2260 | static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { |
| 2261 | [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, |
| 2262 | [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, |
| 2263 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, |
| 2264 | [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, |
| 2265 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, |
| 2266 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, |
Jiri Benc | 9354d45 | 2017-11-02 17:04:37 -0200 | [diff] [blame] | 2267 | [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 }, |
| 2268 | [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 }, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 2269 | }; |
| 2270 | |
stephen hemminger | 48e48a7 | 2014-07-16 11:25:52 -0700 | [diff] [blame] | 2271 | static const struct genl_ops dp_vport_genl_ops[] = { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2272 | { .cmd = OVS_VPORT_CMD_NEW, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 2273 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 2274 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2275 | .doit = ovs_vport_cmd_new |
| 2276 | }, |
| 2277 | { .cmd = OVS_VPORT_CMD_DEL, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 2278 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 2279 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2280 | .doit = ovs_vport_cmd_del |
| 2281 | }, |
| 2282 | { .cmd = OVS_VPORT_CMD_GET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 2283 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2284 | .flags = 0, /* OK for unprivileged users. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2285 | .doit = ovs_vport_cmd_get, |
| 2286 | .dumpit = ovs_vport_cmd_dump |
| 2287 | }, |
| 2288 | { .cmd = OVS_VPORT_CMD_SET, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 2289 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Tycho Andersen | 4a92602 | 2016-02-05 09:20:52 -0700 | [diff] [blame] | 2290 | .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2291 | .doit = ovs_vport_cmd_set, |
| 2292 | }, |
| 2293 | }; |
| 2294 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 2295 | struct genl_family dp_vport_genl_family __ro_after_init = { |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 2296 | .hdrsize = sizeof(struct ovs_header), |
| 2297 | .name = OVS_VPORT_FAMILY, |
| 2298 | .version = OVS_VPORT_VERSION, |
| 2299 | .maxattr = OVS_VPORT_ATTR_MAX, |
Johannes Berg | 3b0f31f | 2019-03-21 22:51:02 +0100 | [diff] [blame] | 2300 | .policy = vport_policy, |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 2301 | .netnsok = true, |
| 2302 | .parallel_ops = true, |
| 2303 | .ops = dp_vport_genl_ops, |
| 2304 | .n_ops = ARRAY_SIZE(dp_vport_genl_ops), |
| 2305 | .mcgrps = &ovs_dp_vport_multicast_group, |
| 2306 | .n_mcgrps = 1, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 2307 | .module = THIS_MODULE, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2308 | }; |
| 2309 | |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 2310 | static struct genl_family * const dp_genl_families[] = { |
| 2311 | &dp_datapath_genl_family, |
| 2312 | &dp_vport_genl_family, |
| 2313 | &dp_flow_genl_family, |
| 2314 | &dp_packet_genl_family, |
Andy Zhou | 96fbc13 | 2017-11-10 12:09:42 -0800 | [diff] [blame] | 2315 | &dp_meter_genl_family, |
Yi-Hung Wei | 11efd5c | 2018-05-24 17:56:43 -0700 | [diff] [blame] | 2316 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
| 2317 | &dp_ct_limit_genl_family, |
| 2318 | #endif |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2319 | }; |
| 2320 | |
| 2321 | static void dp_unregister_genl(int n_families) |
| 2322 | { |
| 2323 | int i; |
| 2324 | |
| 2325 | for (i = 0; i < n_families; i++) |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 2326 | genl_unregister_family(dp_genl_families[i]); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2327 | } |
| 2328 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 2329 | static int __init dp_register_genl(void) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2330 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2331 | int err; |
| 2332 | int i; |
| 2333 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2334 | for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2335 | |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 2336 | err = genl_register_family(dp_genl_families[i]); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2337 | if (err) |
| 2338 | goto error; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | return 0; |
| 2342 | |
| 2343 | error: |
Pravin B Shelar | 0c200ef | 2014-05-06 16:44:50 -0700 | [diff] [blame] | 2344 | dp_unregister_genl(i); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2345 | return err; |
| 2346 | } |
| 2347 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2348 | static int __net_init ovs_init_net(struct net *net) |
| 2349 | { |
| 2350 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
| 2351 | |
| 2352 | INIT_LIST_HEAD(&ovs_net->dps); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2353 | INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); |
Yi-Hung Wei | 11efd5c | 2018-05-24 17:56:43 -0700 | [diff] [blame] | 2354 | return ovs_ct_init(net); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2355 | } |
| 2356 | |
Pravin B Shelar | 7b4577a | 2015-02-17 11:23:10 -0800 | [diff] [blame] | 2357 | static void __net_exit list_vports_from_net(struct net *net, struct net *dnet, |
| 2358 | struct list_head *head) |
| 2359 | { |
| 2360 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
| 2361 | struct datapath *dp; |
| 2362 | |
| 2363 | list_for_each_entry(dp, &ovs_net->dps, list_node) { |
| 2364 | int i; |
| 2365 | |
| 2366 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
| 2367 | struct vport *vport; |
| 2368 | |
| 2369 | hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) { |
Pravin B Shelar | 7b4577a | 2015-02-17 11:23:10 -0800 | [diff] [blame] | 2370 | if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL) |
| 2371 | continue; |
| 2372 | |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 2373 | if (dev_net(vport->dev) == dnet) |
Pravin B Shelar | 7b4577a | 2015-02-17 11:23:10 -0800 | [diff] [blame] | 2374 | list_add(&vport->detach_list, head); |
| 2375 | } |
| 2376 | } |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | static void __net_exit ovs_exit_net(struct net *dnet) |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2381 | { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2382 | struct datapath *dp, *dp_next; |
Pravin B Shelar | 7b4577a | 2015-02-17 11:23:10 -0800 | [diff] [blame] | 2383 | struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id); |
| 2384 | struct vport *vport, *vport_next; |
| 2385 | struct net *net; |
| 2386 | LIST_HEAD(head); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2387 | |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 2388 | ovs_ct_exit(dnet); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2389 | ovs_lock(); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2390 | list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) |
| 2391 | __dp_destroy(dp); |
Pravin B Shelar | 7b4577a | 2015-02-17 11:23:10 -0800 | [diff] [blame] | 2392 | |
Kirill Tkhai | f0b07bb1 | 2018-03-29 19:20:32 +0300 | [diff] [blame] | 2393 | down_read(&net_rwsem); |
Pravin B Shelar | 7b4577a | 2015-02-17 11:23:10 -0800 | [diff] [blame] | 2394 | for_each_net(net) |
| 2395 | list_vports_from_net(net, dnet, &head); |
Kirill Tkhai | f0b07bb1 | 2018-03-29 19:20:32 +0300 | [diff] [blame] | 2396 | up_read(&net_rwsem); |
Pravin B Shelar | 7b4577a | 2015-02-17 11:23:10 -0800 | [diff] [blame] | 2397 | |
| 2398 | /* Detach all vports from given namespace. */ |
| 2399 | list_for_each_entry_safe(vport, vport_next, &head, detach_list) { |
| 2400 | list_del(&vport->detach_list); |
| 2401 | ovs_dp_detach_port(vport); |
| 2402 | } |
| 2403 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 2404 | ovs_unlock(); |
| 2405 | |
| 2406 | cancel_work_sync(&ovs_net->dp_notify_work); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | static struct pernet_operations ovs_net_ops = { |
| 2410 | .init = ovs_init_net, |
| 2411 | .exit = ovs_exit_net, |
| 2412 | .id = &ovs_net_id, |
| 2413 | .size = sizeof(struct ovs_net), |
| 2414 | }; |
| 2415 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2416 | static int __init dp_init(void) |
| 2417 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2418 | int err; |
| 2419 | |
YOSHIFUJI Hideaki / 吉藤英明 | 3523b29 | 2013-01-09 07:19:55 +0000 | [diff] [blame] | 2420 | 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] | 2421 | |
| 2422 | pr_info("Open vSwitch switching datapath\n"); |
| 2423 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 2424 | err = action_fifos_init(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2425 | if (err) |
| 2426 | goto error; |
| 2427 | |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 2428 | err = ovs_internal_dev_rtnl_link_register(); |
| 2429 | if (err) |
| 2430 | goto error_action_fifos_exit; |
| 2431 | |
Jiri Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 2432 | err = ovs_flow_init(); |
| 2433 | if (err) |
| 2434 | goto error_unreg_rtnl_link; |
| 2435 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2436 | err = ovs_vport_init(); |
| 2437 | if (err) |
| 2438 | goto error_flow_exit; |
| 2439 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2440 | err = register_pernet_device(&ovs_net_ops); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2441 | if (err) |
| 2442 | goto error_vport_exit; |
| 2443 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2444 | err = register_netdevice_notifier(&ovs_dp_device_notifier); |
| 2445 | if (err) |
| 2446 | goto error_netns_exit; |
| 2447 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 2448 | err = ovs_netdev_init(); |
| 2449 | if (err) |
| 2450 | goto error_unreg_notifier; |
| 2451 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2452 | err = dp_register_genl(); |
| 2453 | if (err < 0) |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 2454 | goto error_unreg_netdev; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2455 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2456 | return 0; |
| 2457 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 2458 | error_unreg_netdev: |
| 2459 | ovs_netdev_exit(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2460 | error_unreg_notifier: |
| 2461 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2462 | error_netns_exit: |
| 2463 | unregister_pernet_device(&ovs_net_ops); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2464 | error_vport_exit: |
| 2465 | ovs_vport_exit(); |
| 2466 | error_flow_exit: |
| 2467 | ovs_flow_exit(); |
Jiri Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 2468 | error_unreg_rtnl_link: |
| 2469 | ovs_internal_dev_rtnl_link_unregister(); |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 2470 | error_action_fifos_exit: |
| 2471 | action_fifos_exit(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2472 | error: |
| 2473 | return err; |
| 2474 | } |
| 2475 | |
| 2476 | static void dp_cleanup(void) |
| 2477 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2478 | dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 2479 | ovs_netdev_exit(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2480 | unregister_netdevice_notifier(&ovs_dp_device_notifier); |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 2481 | unregister_pernet_device(&ovs_net_ops); |
| 2482 | rcu_barrier(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2483 | ovs_vport_exit(); |
| 2484 | ovs_flow_exit(); |
Jiri Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 2485 | ovs_internal_dev_rtnl_link_unregister(); |
Andy Zhou | 971427f3 | 2014-09-15 19:37:25 -0700 | [diff] [blame] | 2486 | action_fifos_exit(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | module_init(dp_init); |
| 2490 | module_exit(dp_cleanup); |
| 2491 | |
| 2492 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); |
| 2493 | MODULE_LICENSE("GPL"); |
Thadeu Lima de Souza Cascardo | ed22709 | 2016-09-09 17:42:30 -0300 | [diff] [blame] | 2494 | MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY); |
| 2495 | MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY); |
| 2496 | MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY); |
| 2497 | MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY); |
Andy Zhou | 96fbc13 | 2017-11-10 12:09:42 -0800 | [diff] [blame] | 2498 | MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY); |
Yi-Hung Wei | 11efd5c | 2018-05-24 17:56:43 -0700 | [diff] [blame] | 2499 | MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY); |