Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 1 | /* |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 2 | * Copyright (c) 2007-2014 Nicira, Inc. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of version 2 of the GNU General Public |
| 6 | * License as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | * 02110-1301, USA |
| 17 | */ |
| 18 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/if.h> |
| 21 | #include <linux/if_vlan.h> |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 22 | #include <linux/jhash.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 23 | #include <linux/kernel.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/percpu.h> |
| 27 | #include <linux/rcupdate.h> |
| 28 | #include <linux/rtnetlink.h> |
| 29 | #include <linux/compat.h> |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 30 | #include <net/net_namespace.h> |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 31 | #include <linux/module.h> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 32 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 33 | #include "datapath.h" |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 34 | #include "vport.h" |
| 35 | #include "vport-internal_dev.h" |
| 36 | |
Stephen Hemminger | 443cd88 | 2013-12-17 19:22:48 +0000 | [diff] [blame] | 37 | static void ovs_vport_record_error(struct vport *, |
| 38 | enum vport_err_type err_type); |
| 39 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 40 | static LIST_HEAD(vport_ops_list); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 41 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 42 | /* Protected by RCU read lock for reading, ovs_mutex for writing. */ |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 43 | static struct hlist_head *dev_table; |
| 44 | #define VPORT_HASH_BUCKETS 1024 |
| 45 | |
| 46 | /** |
| 47 | * ovs_vport_init - initialize vport subsystem |
| 48 | * |
| 49 | * Called at module load time to initialize the vport subsystem. |
| 50 | */ |
| 51 | int ovs_vport_init(void) |
| 52 | { |
| 53 | dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head), |
| 54 | GFP_KERNEL); |
| 55 | if (!dev_table) |
| 56 | return -ENOMEM; |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * ovs_vport_exit - shutdown vport subsystem |
| 63 | * |
| 64 | * Called at module exit time to shutdown the vport subsystem. |
| 65 | */ |
| 66 | void ovs_vport_exit(void) |
| 67 | { |
| 68 | kfree(dev_table); |
| 69 | } |
| 70 | |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 71 | static struct hlist_head *hash_bucket(const struct net *net, const char *name) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 72 | { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 73 | unsigned int hash = jhash(name, strlen(name), (unsigned long) net); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 74 | return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)]; |
| 75 | } |
| 76 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 77 | int ovs_vport_ops_register(struct vport_ops *ops) |
| 78 | { |
| 79 | int err = -EEXIST; |
| 80 | struct vport_ops *o; |
| 81 | |
| 82 | ovs_lock(); |
| 83 | list_for_each_entry(o, &vport_ops_list, list) |
| 84 | if (ops->type == o->type) |
| 85 | goto errout; |
| 86 | |
| 87 | list_add_tail(&ops->list, &vport_ops_list); |
| 88 | err = 0; |
| 89 | errout: |
| 90 | ovs_unlock(); |
| 91 | return err; |
| 92 | } |
Pravin B Shelar | 9ba559d | 2014-11-06 06:44:27 -0800 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(ovs_vport_ops_register); |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 94 | |
| 95 | void ovs_vport_ops_unregister(struct vport_ops *ops) |
| 96 | { |
| 97 | ovs_lock(); |
| 98 | list_del(&ops->list); |
| 99 | ovs_unlock(); |
| 100 | } |
Pravin B Shelar | 9ba559d | 2014-11-06 06:44:27 -0800 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(ovs_vport_ops_unregister); |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 102 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 103 | /** |
| 104 | * ovs_vport_locate - find a port that has already been created |
| 105 | * |
| 106 | * @name: name of port to find |
| 107 | * |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 108 | * Must be called with ovs or RCU read lock. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 109 | */ |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 110 | struct vport *ovs_vport_locate(const struct net *net, const char *name) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 111 | { |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 112 | struct hlist_head *bucket = hash_bucket(net, name); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 113 | struct vport *vport; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 114 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 115 | hlist_for_each_entry_rcu(vport, bucket, hash_node) |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 116 | if (!strcmp(name, vport->ops->get_name(vport)) && |
| 117 | net_eq(ovs_dp_get_net(vport->dp), net)) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 118 | return vport; |
| 119 | |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * ovs_vport_alloc - allocate and initialize new vport |
| 125 | * |
| 126 | * @priv_size: Size of private data area to allocate. |
| 127 | * @ops: vport device ops |
| 128 | * |
| 129 | * Allocate and initialize a new vport defined by @ops. The vport will contain |
| 130 | * a private data area of size @priv_size that can be accessed using |
| 131 | * vport_priv(). vports that are no longer needed should be released with |
| 132 | * vport_free(). |
| 133 | */ |
| 134 | struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops, |
| 135 | const struct vport_parms *parms) |
| 136 | { |
| 137 | struct vport *vport; |
| 138 | size_t alloc_size; |
| 139 | |
| 140 | alloc_size = sizeof(struct vport); |
| 141 | if (priv_size) { |
| 142 | alloc_size = ALIGN(alloc_size, VPORT_ALIGN); |
| 143 | alloc_size += priv_size; |
| 144 | } |
| 145 | |
| 146 | vport = kzalloc(alloc_size, GFP_KERNEL); |
| 147 | if (!vport) |
| 148 | return ERR_PTR(-ENOMEM); |
| 149 | |
| 150 | vport->dp = parms->dp; |
| 151 | vport->port_no = parms->port_no; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 152 | vport->ops = ops; |
Pravin B Shelar | 15eac2a | 2012-08-23 12:40:54 -0700 | [diff] [blame] | 153 | INIT_HLIST_NODE(&vport->dp_hash_node); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 154 | |
Christoph Jaeger | 3791b3f | 2014-08-12 09:27:57 +0200 | [diff] [blame] | 155 | if (ovs_vport_set_upcall_portids(vport, parms->upcall_portids)) { |
| 156 | kfree(vport); |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 157 | return ERR_PTR(-EINVAL); |
Christoph Jaeger | 3791b3f | 2014-08-12 09:27:57 +0200 | [diff] [blame] | 158 | } |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 159 | |
WANG Cong | 1c213bd | 2014-02-13 11:46:28 -0800 | [diff] [blame] | 160 | vport->percpu_stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); |
Dan Carpenter | f0a98ae | 2011-12-05 20:27:07 +0000 | [diff] [blame] | 161 | if (!vport->percpu_stats) { |
| 162 | kfree(vport); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 163 | return ERR_PTR(-ENOMEM); |
Dan Carpenter | f0a98ae | 2011-12-05 20:27:07 +0000 | [diff] [blame] | 164 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 165 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 166 | return vport; |
| 167 | } |
Pravin B Shelar | 9ba559d | 2014-11-06 06:44:27 -0800 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(ovs_vport_alloc); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * ovs_vport_free - uninitialize and free vport |
| 172 | * |
| 173 | * @vport: vport to free |
| 174 | * |
| 175 | * Frees a vport allocated with vport_alloc() when it is no longer needed. |
| 176 | * |
| 177 | * The caller must ensure that an RCU grace period has passed since the last |
| 178 | * time @vport was in a datapath. |
| 179 | */ |
| 180 | void ovs_vport_free(struct vport *vport) |
| 181 | { |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 182 | /* vport is freed from RCU callback or error path, Therefore |
| 183 | * it is safe to use raw dereference. |
| 184 | */ |
| 185 | kfree(rcu_dereference_raw(vport->upcall_portids)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 186 | free_percpu(vport->percpu_stats); |
| 187 | kfree(vport); |
| 188 | } |
Pravin B Shelar | 9ba559d | 2014-11-06 06:44:27 -0800 | [diff] [blame] | 189 | EXPORT_SYMBOL_GPL(ovs_vport_free); |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 190 | |
| 191 | static struct vport_ops *ovs_vport_lookup(const struct vport_parms *parms) |
| 192 | { |
| 193 | struct vport_ops *ops; |
| 194 | |
| 195 | list_for_each_entry(ops, &vport_ops_list, list) |
| 196 | if (ops->type == parms->type) |
| 197 | return ops; |
| 198 | |
| 199 | return NULL; |
| 200 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * ovs_vport_add - add vport device (for kernel callers) |
| 204 | * |
| 205 | * @parms: Information about new vport. |
| 206 | * |
| 207 | * Creates a new vport with the specified configuration (which is dependent on |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 208 | * device type). ovs_mutex must be held. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 209 | */ |
| 210 | struct vport *ovs_vport_add(const struct vport_parms *parms) |
| 211 | { |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 212 | struct vport_ops *ops; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 213 | struct vport *vport; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 214 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 215 | ops = ovs_vport_lookup(parms); |
| 216 | if (ops) { |
| 217 | struct hlist_head *bucket; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 218 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 219 | if (!try_module_get(ops->owner)) |
| 220 | return ERR_PTR(-EAFNOSUPPORT); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 221 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 222 | vport = ops->create(parms); |
| 223 | if (IS_ERR(vport)) { |
| 224 | module_put(ops->owner); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 225 | return vport; |
| 226 | } |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 227 | |
| 228 | bucket = hash_bucket(ovs_dp_get_net(vport->dp), |
| 229 | vport->ops->get_name(vport)); |
| 230 | hlist_add_head_rcu(&vport->hash_node, bucket); |
| 231 | return vport; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 234 | /* Unlock to attempt module load and return -EAGAIN if load |
| 235 | * was successful as we need to restart the port addition |
| 236 | * workflow. |
| 237 | */ |
| 238 | ovs_unlock(); |
| 239 | request_module("vport-type-%d", parms->type); |
| 240 | ovs_lock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 241 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 242 | if (!ovs_vport_lookup(parms)) |
| 243 | return ERR_PTR(-EAFNOSUPPORT); |
| 244 | else |
| 245 | return ERR_PTR(-EAGAIN); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * ovs_vport_set_options - modify existing vport device (for kernel callers) |
| 250 | * |
| 251 | * @vport: vport to modify. |
Justin Pettit | 2694838 | 2013-08-19 17:49:29 -0700 | [diff] [blame] | 252 | * @options: New configuration. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 253 | * |
| 254 | * Modifies an existing device with the specified configuration (which is |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 255 | * dependent on device type). ovs_mutex must be held. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 256 | */ |
| 257 | int ovs_vport_set_options(struct vport *vport, struct nlattr *options) |
| 258 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 259 | if (!vport->ops->set_options) |
| 260 | return -EOPNOTSUPP; |
| 261 | return vport->ops->set_options(vport, options); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * ovs_vport_del - delete existing vport device |
| 266 | * |
| 267 | * @vport: vport to delete. |
| 268 | * |
| 269 | * Detaches @vport from its datapath and destroys it. It is possible to fail |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 270 | * for reasons such as lack of memory. ovs_mutex must be held. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 271 | */ |
| 272 | void ovs_vport_del(struct vport *vport) |
| 273 | { |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 274 | ASSERT_OVSL(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 275 | |
| 276 | hlist_del_rcu(&vport->hash_node); |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 277 | module_put(vport->ops->owner); |
Thomas Graf | fa2d8ff | 2015-03-30 13:57:41 +0200 | [diff] [blame] | 278 | vport->ops->destroy(vport); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
| 282 | * ovs_vport_get_stats - retrieve device stats |
| 283 | * |
| 284 | * @vport: vport from which to retrieve the stats |
| 285 | * @stats: location to store stats |
| 286 | * |
| 287 | * Retrieves transmit, receive, and error stats for the given device. |
| 288 | * |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 289 | * Must be called with ovs_mutex or rcu_read_lock. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 290 | */ |
| 291 | void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats) |
| 292 | { |
| 293 | int i; |
| 294 | |
| 295 | memset(stats, 0, sizeof(*stats)); |
| 296 | |
| 297 | /* We potentially have 2 sources of stats that need to be combined: |
| 298 | * those we have collected (split into err_stats and percpu_stats) from |
| 299 | * set_stats() and device error stats from netdev->get_stats() (for |
| 300 | * errors that happen downstream and therefore aren't reported through |
| 301 | * our vport_record_error() function). |
| 302 | * Stats from first source are reported by ovs (OVS_VPORT_ATTR_STATS). |
| 303 | * netdev-stats can be directly read over netlink-ioctl. |
| 304 | */ |
| 305 | |
Li RongQing | e403ade | 2014-09-06 19:06:11 +0800 | [diff] [blame] | 306 | stats->rx_errors = atomic_long_read(&vport->err_stats.rx_errors); |
| 307 | stats->tx_errors = atomic_long_read(&vport->err_stats.tx_errors); |
| 308 | stats->tx_dropped = atomic_long_read(&vport->err_stats.tx_dropped); |
| 309 | stats->rx_dropped = atomic_long_read(&vport->err_stats.rx_dropped); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 310 | |
| 311 | for_each_possible_cpu(i) { |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 312 | const struct pcpu_sw_netstats *percpu_stats; |
| 313 | struct pcpu_sw_netstats local_stats; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 314 | unsigned int start; |
| 315 | |
| 316 | percpu_stats = per_cpu_ptr(vport->percpu_stats, i); |
| 317 | |
| 318 | do { |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 319 | start = u64_stats_fetch_begin_irq(&percpu_stats->syncp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 320 | local_stats = *percpu_stats; |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 321 | } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 322 | |
| 323 | stats->rx_bytes += local_stats.rx_bytes; |
| 324 | stats->rx_packets += local_stats.rx_packets; |
| 325 | stats->tx_bytes += local_stats.tx_bytes; |
| 326 | stats->tx_packets += local_stats.tx_packets; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * ovs_vport_get_options - retrieve device options |
| 332 | * |
| 333 | * @vport: vport from which to retrieve the options. |
| 334 | * @skb: sk_buff where options should be appended. |
| 335 | * |
| 336 | * Retrieves the configuration of the given device, appending an |
| 337 | * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested |
| 338 | * vport-specific attributes to @skb. |
| 339 | * |
| 340 | * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another |
| 341 | * negative error code if a real error occurred. If an error occurs, @skb is |
| 342 | * left unmodified. |
| 343 | * |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 344 | * Must be called with ovs_mutex or rcu_read_lock. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 345 | */ |
| 346 | int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb) |
| 347 | { |
| 348 | struct nlattr *nla; |
Thomas Graf | 5d96335 | 2013-04-03 00:30:43 +0200 | [diff] [blame] | 349 | int err; |
| 350 | |
| 351 | if (!vport->ops->get_options) |
| 352 | return 0; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 353 | |
| 354 | nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS); |
| 355 | if (!nla) |
| 356 | return -EMSGSIZE; |
| 357 | |
Thomas Graf | 5d96335 | 2013-04-03 00:30:43 +0200 | [diff] [blame] | 358 | err = vport->ops->get_options(vport, skb); |
| 359 | if (err) { |
| 360 | nla_nest_cancel(skb, nla); |
| 361 | return err; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | nla_nest_end(skb, nla); |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /** |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 369 | * ovs_vport_set_upcall_portids - set upcall portids of @vport. |
| 370 | * |
| 371 | * @vport: vport to modify. |
| 372 | * @ids: new configuration, an array of port ids. |
| 373 | * |
| 374 | * Sets the vport's upcall_portids to @ids. |
| 375 | * |
| 376 | * Returns 0 if successful, -EINVAL if @ids is zero length or cannot be parsed |
| 377 | * as an array of U32. |
| 378 | * |
| 379 | * Must be called with ovs_mutex. |
| 380 | */ |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 381 | int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids) |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 382 | { |
| 383 | struct vport_portids *old, *vport_portids; |
| 384 | |
| 385 | if (!nla_len(ids) || nla_len(ids) % sizeof(u32)) |
| 386 | return -EINVAL; |
| 387 | |
| 388 | old = ovsl_dereference(vport->upcall_portids); |
| 389 | |
| 390 | vport_portids = kmalloc(sizeof(*vport_portids) + nla_len(ids), |
| 391 | GFP_KERNEL); |
| 392 | if (!vport_portids) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | vport_portids->n_ids = nla_len(ids) / sizeof(u32); |
| 396 | vport_portids->rn_ids = reciprocal_value(vport_portids->n_ids); |
| 397 | nla_memcpy(vport_portids->ids, ids, nla_len(ids)); |
| 398 | |
| 399 | rcu_assign_pointer(vport->upcall_portids, vport_portids); |
| 400 | |
| 401 | if (old) |
| 402 | kfree_rcu(old, rcu); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * ovs_vport_get_upcall_portids - get the upcall_portids of @vport. |
| 408 | * |
| 409 | * @vport: vport from which to retrieve the portids. |
| 410 | * @skb: sk_buff where portids should be appended. |
| 411 | * |
| 412 | * Retrieves the configuration of the given vport, appending the |
| 413 | * %OVS_VPORT_ATTR_UPCALL_PID attribute which is the array of upcall |
| 414 | * portids to @skb. |
| 415 | * |
| 416 | * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room. |
| 417 | * If an error occurs, @skb is left unmodified. Must be called with |
| 418 | * ovs_mutex or rcu_read_lock. |
| 419 | */ |
| 420 | int ovs_vport_get_upcall_portids(const struct vport *vport, |
| 421 | struct sk_buff *skb) |
| 422 | { |
| 423 | struct vport_portids *ids; |
| 424 | |
| 425 | ids = rcu_dereference_ovsl(vport->upcall_portids); |
| 426 | |
| 427 | if (vport->dp->user_features & OVS_DP_F_VPORT_PIDS) |
| 428 | return nla_put(skb, OVS_VPORT_ATTR_UPCALL_PID, |
| 429 | ids->n_ids * sizeof(u32), (void *)ids->ids); |
| 430 | else |
| 431 | return nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, ids->ids[0]); |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * ovs_vport_find_upcall_portid - find the upcall portid to send upcall. |
| 436 | * |
| 437 | * @vport: vport from which the missed packet is received. |
| 438 | * @skb: skb that the missed packet was received. |
| 439 | * |
| 440 | * Uses the skb_get_hash() to select the upcall portid to send the |
| 441 | * upcall. |
| 442 | * |
| 443 | * Returns the portid of the target socket. Must be called with rcu_read_lock. |
| 444 | */ |
Fabian Frederick | 4e8febd | 2014-10-15 21:03:41 +0200 | [diff] [blame] | 445 | u32 ovs_vport_find_upcall_portid(const struct vport *vport, struct sk_buff *skb) |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 446 | { |
| 447 | struct vport_portids *ids; |
| 448 | u32 ids_index; |
| 449 | u32 hash; |
| 450 | |
Fabian Frederick | 4e8febd | 2014-10-15 21:03:41 +0200 | [diff] [blame] | 451 | ids = rcu_dereference(vport->upcall_portids); |
Alex Wang | 5cd667b | 2014-07-17 15:14:13 -0700 | [diff] [blame] | 452 | |
| 453 | if (ids->n_ids == 1 && ids->ids[0] == 0) |
| 454 | return 0; |
| 455 | |
| 456 | hash = skb_get_hash(skb); |
| 457 | ids_index = hash - ids->n_ids * reciprocal_divide(hash, ids->rn_ids); |
| 458 | return ids->ids[ids_index]; |
| 459 | } |
| 460 | |
| 461 | /** |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 462 | * ovs_vport_receive - pass up received packet to the datapath for processing |
| 463 | * |
| 464 | * @vport: vport that received the packet |
| 465 | * @skb: skb that was received |
Justin Pettit | 2694838 | 2013-08-19 17:49:29 -0700 | [diff] [blame] | 466 | * @tun_key: tunnel (if any) that carried packet |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 467 | * |
| 468 | * Must be called with rcu_read_lock. The packet cannot be shared and |
Cong Wang | d176ca2 | 2013-02-22 19:41:26 +0800 | [diff] [blame] | 469 | * skb->data should point to the Ethernet header. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 470 | */ |
Pravin B Shelar | 7d5437c | 2013-06-17 17:50:18 -0700 | [diff] [blame] | 471 | void ovs_vport_receive(struct vport *vport, struct sk_buff *skb, |
Thomas Graf | 12eb18f | 2014-11-06 06:58:52 -0800 | [diff] [blame] | 472 | const struct ovs_tunnel_info *tun_info) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 473 | { |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 474 | struct pcpu_sw_netstats *stats; |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 475 | struct sw_flow_key key; |
| 476 | int error; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 477 | |
Shan Wei | 404f2f1 | 2012-11-13 09:52:25 +0800 | [diff] [blame] | 478 | stats = this_cpu_ptr(vport->percpu_stats); |
Pravin B Shelar | e0f0ecf | 2013-04-15 13:30:37 -0700 | [diff] [blame] | 479 | u64_stats_update_begin(&stats->syncp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 480 | stats->rx_packets++; |
Jiri Pirko | df8a39d | 2015-01-13 17:13:44 +0100 | [diff] [blame] | 481 | stats->rx_bytes += skb->len + |
| 482 | (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); |
Pravin B Shelar | e0f0ecf | 2013-04-15 13:30:37 -0700 | [diff] [blame] | 483 | u64_stats_update_end(&stats->syncp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 484 | |
Pravin B Shelar | 83c8df2 | 2014-09-15 19:20:31 -0700 | [diff] [blame] | 485 | OVS_CB(skb)->input_vport = vport; |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 486 | OVS_CB(skb)->egress_tun_info = NULL; |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 487 | /* Extract flow from 'skb' into 'key'. */ |
Jesse Gross | f0b128c | 2014-10-03 15:35:31 -0700 | [diff] [blame] | 488 | error = ovs_flow_key_extract(tun_info, skb, &key); |
Pravin B Shelar | 8c8b1b8 | 2014-09-15 19:28:44 -0700 | [diff] [blame] | 489 | if (unlikely(error)) { |
| 490 | kfree_skb(skb); |
| 491 | return; |
| 492 | } |
| 493 | ovs_dp_process_packet(skb, &key); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 494 | } |
Pravin B Shelar | 9ba559d | 2014-11-06 06:44:27 -0800 | [diff] [blame] | 495 | EXPORT_SYMBOL_GPL(ovs_vport_receive); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 496 | |
| 497 | /** |
| 498 | * ovs_vport_send - send a packet on a device |
| 499 | * |
| 500 | * @vport: vport on which to send the packet |
| 501 | * @skb: skb to send |
| 502 | * |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 503 | * Sends the given packet and returns the length of data sent. Either ovs |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 504 | * lock or rcu_read_lock must be held. |
| 505 | */ |
| 506 | int ovs_vport_send(struct vport *vport, struct sk_buff *skb) |
| 507 | { |
| 508 | int sent = vport->ops->send(vport, skb); |
| 509 | |
Pravin B Shelar | 91b7514 | 2013-05-13 08:22:34 -0700 | [diff] [blame] | 510 | if (likely(sent > 0)) { |
Li RongQing | 8f84985 | 2014-01-04 13:57:59 +0800 | [diff] [blame] | 511 | struct pcpu_sw_netstats *stats; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 512 | |
Shan Wei | 404f2f1 | 2012-11-13 09:52:25 +0800 | [diff] [blame] | 513 | stats = this_cpu_ptr(vport->percpu_stats); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 514 | |
Pravin B Shelar | e0f0ecf | 2013-04-15 13:30:37 -0700 | [diff] [blame] | 515 | u64_stats_update_begin(&stats->syncp); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 516 | stats->tx_packets++; |
| 517 | stats->tx_bytes += sent; |
Pravin B Shelar | e0f0ecf | 2013-04-15 13:30:37 -0700 | [diff] [blame] | 518 | u64_stats_update_end(&stats->syncp); |
Pravin B Shelar | 91b7514 | 2013-05-13 08:22:34 -0700 | [diff] [blame] | 519 | } else if (sent < 0) { |
| 520 | ovs_vport_record_error(vport, VPORT_E_TX_ERROR); |
Pravin B Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame] | 521 | } else { |
Pravin B Shelar | 91b7514 | 2013-05-13 08:22:34 -0700 | [diff] [blame] | 522 | ovs_vport_record_error(vport, VPORT_E_TX_DROPPED); |
Pravin B Shelar | 997e068 | 2014-12-23 16:20:32 -0800 | [diff] [blame] | 523 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 524 | return sent; |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * ovs_vport_record_error - indicate device error to generic stats layer |
| 529 | * |
| 530 | * @vport: vport that encountered the error |
| 531 | * @err_type: one of enum vport_err_type types to indicate the error type |
| 532 | * |
| 533 | * If using the vport generic stats layer indicate that an error of the given |
Andy Hill | af78416 | 2013-06-07 16:53:50 -0700 | [diff] [blame] | 534 | * type has occurred. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 535 | */ |
Stephen Hemminger | 443cd88 | 2013-12-17 19:22:48 +0000 | [diff] [blame] | 536 | static void ovs_vport_record_error(struct vport *vport, |
| 537 | enum vport_err_type err_type) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 538 | { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 539 | switch (err_type) { |
| 540 | case VPORT_E_RX_DROPPED: |
Li RongQing | e403ade | 2014-09-06 19:06:11 +0800 | [diff] [blame] | 541 | atomic_long_inc(&vport->err_stats.rx_dropped); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 542 | break; |
| 543 | |
| 544 | case VPORT_E_RX_ERROR: |
Li RongQing | e403ade | 2014-09-06 19:06:11 +0800 | [diff] [blame] | 545 | atomic_long_inc(&vport->err_stats.rx_errors); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 546 | break; |
| 547 | |
| 548 | case VPORT_E_TX_DROPPED: |
Li RongQing | e403ade | 2014-09-06 19:06:11 +0800 | [diff] [blame] | 549 | atomic_long_inc(&vport->err_stats.tx_dropped); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 550 | break; |
| 551 | |
| 552 | case VPORT_E_TX_ERROR: |
Li RongQing | e403ade | 2014-09-06 19:06:11 +0800 | [diff] [blame] | 553 | atomic_long_inc(&vport->err_stats.tx_errors); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 554 | break; |
Peter Senna Tschudin | a2bf91b | 2012-09-18 07:10:44 +0000 | [diff] [blame] | 555 | } |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 556 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 557 | } |
Pravin B Shelar | aa31070 | 2013-06-17 17:50:33 -0700 | [diff] [blame] | 558 | |
| 559 | static void free_vport_rcu(struct rcu_head *rcu) |
| 560 | { |
| 561 | struct vport *vport = container_of(rcu, struct vport, rcu); |
| 562 | |
| 563 | ovs_vport_free(vport); |
| 564 | } |
| 565 | |
| 566 | void ovs_vport_deferred_free(struct vport *vport) |
| 567 | { |
| 568 | if (!vport) |
| 569 | return; |
| 570 | |
| 571 | call_rcu(&vport->rcu, free_vport_rcu); |
| 572 | } |
Pravin B Shelar | 9ba559d | 2014-11-06 06:44:27 -0800 | [diff] [blame] | 573 | EXPORT_SYMBOL_GPL(ovs_vport_deferred_free); |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 574 | |
| 575 | int ovs_tunnel_get_egress_info(struct ovs_tunnel_info *egress_tun_info, |
| 576 | struct net *net, |
| 577 | const struct ovs_tunnel_info *tun_info, |
| 578 | u8 ipproto, |
| 579 | u32 skb_mark, |
| 580 | __be16 tp_src, |
| 581 | __be16 tp_dst) |
| 582 | { |
| 583 | const struct ovs_key_ipv4_tunnel *tun_key; |
| 584 | struct rtable *rt; |
| 585 | struct flowi4 fl; |
| 586 | |
| 587 | if (unlikely(!tun_info)) |
| 588 | return -EINVAL; |
| 589 | |
| 590 | tun_key = &tun_info->tunnel; |
| 591 | |
| 592 | /* Route lookup to get srouce IP address. |
| 593 | * The process may need to be changed if the corresponding process |
| 594 | * in vports ops changed. |
| 595 | */ |
Fan Du | 3f4c1d8 | 2015-01-14 13:10:35 +0800 | [diff] [blame] | 596 | rt = ovs_tunnel_route_lookup(net, tun_key, skb_mark, &fl, ipproto); |
Wenyu Zhang | 8f0aad6 | 2014-11-06 06:51:24 -0800 | [diff] [blame] | 597 | if (IS_ERR(rt)) |
| 598 | return PTR_ERR(rt); |
| 599 | |
| 600 | ip_rt_put(rt); |
| 601 | |
| 602 | /* Generate egress_tun_info based on tun_info, |
| 603 | * saddr, tp_src and tp_dst |
| 604 | */ |
| 605 | __ovs_flow_tun_info_init(egress_tun_info, |
| 606 | fl.saddr, tun_key->ipv4_dst, |
| 607 | tun_key->ipv4_tos, |
| 608 | tun_key->ipv4_ttl, |
| 609 | tp_src, tp_dst, |
| 610 | tun_key->tun_id, |
| 611 | tun_key->tun_flags, |
| 612 | tun_info->options, |
| 613 | tun_info->options_len); |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | EXPORT_SYMBOL_GPL(ovs_tunnel_get_egress_info); |
| 618 | |
| 619 | int ovs_vport_get_egress_tun_info(struct vport *vport, struct sk_buff *skb, |
| 620 | struct ovs_tunnel_info *info) |
| 621 | { |
| 622 | /* get_egress_tun_info() is only implemented on tunnel ports. */ |
| 623 | if (unlikely(!vport->ops->get_egress_tun_info)) |
| 624 | return -EINVAL; |
| 625 | |
| 626 | return vport->ops->get_egress_tun_info(vport, skb, info); |
| 627 | } |