blob: 9584526c077804f21d22726d4bb5bd7cc4260d36 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Pravin B Shelar83c8df22014-09-15 19:20:31 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
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 Grossccb13522011-10-25 19:26:31 -070019#include <linux/etherdevice.h>
20#include <linux/if.h>
21#include <linux/if_vlan.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080022#include <linux/jhash.h>
Jesse Grossccb13522011-10-25 19:26:31 -070023#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 Shelar46df7b82012-02-22 19:58:59 -080030#include <net/net_namespace.h>
Thomas Graf62b9c8d2014-10-22 17:29:06 +020031#include <linux/module.h>
Jesse Grossccb13522011-10-25 19:26:31 -070032
Pravin B Shelar46df7b82012-02-22 19:58:59 -080033#include "datapath.h"
Jesse Grossccb13522011-10-25 19:26:31 -070034#include "vport.h"
35#include "vport-internal_dev.h"
36
Stephen Hemminger443cd882013-12-17 19:22:48 +000037static void ovs_vport_record_error(struct vport *,
38 enum vport_err_type err_type);
39
Thomas Graf62b9c8d2014-10-22 17:29:06 +020040static LIST_HEAD(vport_ops_list);
Jesse Grossccb13522011-10-25 19:26:31 -070041
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070042/* Protected by RCU read lock for reading, ovs_mutex for writing. */
Jesse Grossccb13522011-10-25 19:26:31 -070043static 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 */
51int 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 */
66void ovs_vport_exit(void)
67{
68 kfree(dev_table);
69}
70
Thomas Graf12eb18f2014-11-06 06:58:52 -080071static struct hlist_head *hash_bucket(const struct net *net, const char *name)
Jesse Grossccb13522011-10-25 19:26:31 -070072{
Pravin B Shelar46df7b82012-02-22 19:58:59 -080073 unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
Jesse Grossccb13522011-10-25 19:26:31 -070074 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
75}
76
Thomas Graf62b9c8d2014-10-22 17:29:06 +020077int 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;
89errout:
90 ovs_unlock();
91 return err;
92}
Pravin B Shelar9ba559d2014-11-06 06:44:27 -080093EXPORT_SYMBOL_GPL(ovs_vport_ops_register);
Thomas Graf62b9c8d2014-10-22 17:29:06 +020094
95void ovs_vport_ops_unregister(struct vport_ops *ops)
96{
97 ovs_lock();
98 list_del(&ops->list);
99 ovs_unlock();
100}
Pravin B Shelar9ba559d2014-11-06 06:44:27 -0800101EXPORT_SYMBOL_GPL(ovs_vport_ops_unregister);
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200102
Jesse Grossccb13522011-10-25 19:26:31 -0700103/**
104 * ovs_vport_locate - find a port that has already been created
105 *
106 * @name: name of port to find
107 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700108 * Must be called with ovs or RCU read lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700109 */
Thomas Graf12eb18f2014-11-06 06:58:52 -0800110struct vport *ovs_vport_locate(const struct net *net, const char *name)
Jesse Grossccb13522011-10-25 19:26:31 -0700111{
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800112 struct hlist_head *bucket = hash_bucket(net, name);
Jesse Grossccb13522011-10-25 19:26:31 -0700113 struct vport *vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700114
Sasha Levinb67bfe02013-02-27 17:06:00 -0800115 hlist_for_each_entry_rcu(vport, bucket, hash_node)
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800116 if (!strcmp(name, vport->ops->get_name(vport)) &&
117 net_eq(ovs_dp_get_net(vport->dp), net))
Jesse Grossccb13522011-10-25 19:26:31 -0700118 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 */
134struct 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 Grossccb13522011-10-25 19:26:31 -0700152 vport->ops = ops;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700153 INIT_HLIST_NODE(&vport->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700154
Christoph Jaeger3791b3f2014-08-12 09:27:57 +0200155 if (ovs_vport_set_upcall_portids(vport, parms->upcall_portids)) {
156 kfree(vport);
Alex Wang5cd667b2014-07-17 15:14:13 -0700157 return ERR_PTR(-EINVAL);
Christoph Jaeger3791b3f2014-08-12 09:27:57 +0200158 }
Alex Wang5cd667b2014-07-17 15:14:13 -0700159
WANG Cong1c213bd2014-02-13 11:46:28 -0800160 vport->percpu_stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
Dan Carpenterf0a98ae2011-12-05 20:27:07 +0000161 if (!vport->percpu_stats) {
162 kfree(vport);
Jesse Grossccb13522011-10-25 19:26:31 -0700163 return ERR_PTR(-ENOMEM);
Dan Carpenterf0a98ae2011-12-05 20:27:07 +0000164 }
Jesse Grossccb13522011-10-25 19:26:31 -0700165
Jesse Grossccb13522011-10-25 19:26:31 -0700166 return vport;
167}
Pravin B Shelar9ba559d2014-11-06 06:44:27 -0800168EXPORT_SYMBOL_GPL(ovs_vport_alloc);
Jesse Grossccb13522011-10-25 19:26:31 -0700169
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 */
180void ovs_vport_free(struct vport *vport)
181{
Alex Wang5cd667b2014-07-17 15:14:13 -0700182 /* 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 Grossccb13522011-10-25 19:26:31 -0700186 free_percpu(vport->percpu_stats);
187 kfree(vport);
188}
Pravin B Shelar9ba559d2014-11-06 06:44:27 -0800189EXPORT_SYMBOL_GPL(ovs_vport_free);
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200190
191static 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 Grossccb13522011-10-25 19:26:31 -0700201
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 Shelar8e4e1712013-04-15 13:23:03 -0700208 * device type). ovs_mutex must be held.
Jesse Grossccb13522011-10-25 19:26:31 -0700209 */
210struct vport *ovs_vport_add(const struct vport_parms *parms)
211{
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200212 struct vport_ops *ops;
Jesse Grossccb13522011-10-25 19:26:31 -0700213 struct vport *vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700214
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200215 ops = ovs_vport_lookup(parms);
216 if (ops) {
217 struct hlist_head *bucket;
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800218
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200219 if (!try_module_get(ops->owner))
220 return ERR_PTR(-EAFNOSUPPORT);
Jesse Grossccb13522011-10-25 19:26:31 -0700221
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200222 vport = ops->create(parms);
223 if (IS_ERR(vport)) {
224 module_put(ops->owner);
Jesse Grossccb13522011-10-25 19:26:31 -0700225 return vport;
226 }
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200227
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 Grossccb13522011-10-25 19:26:31 -0700232 }
233
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200234 /* 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 Grossccb13522011-10-25 19:26:31 -0700241
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200242 if (!ovs_vport_lookup(parms))
243 return ERR_PTR(-EAFNOSUPPORT);
244 else
245 return ERR_PTR(-EAGAIN);
Jesse Grossccb13522011-10-25 19:26:31 -0700246}
247
248/**
249 * ovs_vport_set_options - modify existing vport device (for kernel callers)
250 *
251 * @vport: vport to modify.
Justin Pettit26948382013-08-19 17:49:29 -0700252 * @options: New configuration.
Jesse Grossccb13522011-10-25 19:26:31 -0700253 *
254 * Modifies an existing device with the specified configuration (which is
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700255 * dependent on device type). ovs_mutex must be held.
Jesse Grossccb13522011-10-25 19:26:31 -0700256 */
257int ovs_vport_set_options(struct vport *vport, struct nlattr *options)
258{
Jesse Grossccb13522011-10-25 19:26:31 -0700259 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 Shelar8e4e1712013-04-15 13:23:03 -0700270 * for reasons such as lack of memory. ovs_mutex must be held.
Jesse Grossccb13522011-10-25 19:26:31 -0700271 */
272void ovs_vport_del(struct vport *vport)
273{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700274 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700275
276 hlist_del_rcu(&vport->hash_node);
277
278 vport->ops->destroy(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200279
280 module_put(vport->ops->owner);
Jesse Grossccb13522011-10-25 19:26:31 -0700281}
282
283/**
284 * ovs_vport_get_stats - retrieve device stats
285 *
286 * @vport: vport from which to retrieve the stats
287 * @stats: location to store stats
288 *
289 * Retrieves transmit, receive, and error stats for the given device.
290 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700291 * Must be called with ovs_mutex or rcu_read_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700292 */
293void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
294{
295 int i;
296
297 memset(stats, 0, sizeof(*stats));
298
299 /* We potentially have 2 sources of stats that need to be combined:
300 * those we have collected (split into err_stats and percpu_stats) from
301 * set_stats() and device error stats from netdev->get_stats() (for
302 * errors that happen downstream and therefore aren't reported through
303 * our vport_record_error() function).
304 * Stats from first source are reported by ovs (OVS_VPORT_ATTR_STATS).
305 * netdev-stats can be directly read over netlink-ioctl.
306 */
307
Li RongQinge403ade2014-09-06 19:06:11 +0800308 stats->rx_errors = atomic_long_read(&vport->err_stats.rx_errors);
309 stats->tx_errors = atomic_long_read(&vport->err_stats.tx_errors);
310 stats->tx_dropped = atomic_long_read(&vport->err_stats.tx_dropped);
311 stats->rx_dropped = atomic_long_read(&vport->err_stats.rx_dropped);
Jesse Grossccb13522011-10-25 19:26:31 -0700312
313 for_each_possible_cpu(i) {
Li RongQing8f849852014-01-04 13:57:59 +0800314 const struct pcpu_sw_netstats *percpu_stats;
315 struct pcpu_sw_netstats local_stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700316 unsigned int start;
317
318 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
319
320 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700321 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700322 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700323 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700324
325 stats->rx_bytes += local_stats.rx_bytes;
326 stats->rx_packets += local_stats.rx_packets;
327 stats->tx_bytes += local_stats.tx_bytes;
328 stats->tx_packets += local_stats.tx_packets;
329 }
330}
331
332/**
333 * ovs_vport_get_options - retrieve device options
334 *
335 * @vport: vport from which to retrieve the options.
336 * @skb: sk_buff where options should be appended.
337 *
338 * Retrieves the configuration of the given device, appending an
339 * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
340 * vport-specific attributes to @skb.
341 *
342 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
343 * negative error code if a real error occurred. If an error occurs, @skb is
344 * left unmodified.
345 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700346 * Must be called with ovs_mutex or rcu_read_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700347 */
348int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
349{
350 struct nlattr *nla;
Thomas Graf5d963352013-04-03 00:30:43 +0200351 int err;
352
353 if (!vport->ops->get_options)
354 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700355
356 nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS);
357 if (!nla)
358 return -EMSGSIZE;
359
Thomas Graf5d963352013-04-03 00:30:43 +0200360 err = vport->ops->get_options(vport, skb);
361 if (err) {
362 nla_nest_cancel(skb, nla);
363 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700364 }
365
366 nla_nest_end(skb, nla);
367 return 0;
368}
369
370/**
Alex Wang5cd667b2014-07-17 15:14:13 -0700371 * ovs_vport_set_upcall_portids - set upcall portids of @vport.
372 *
373 * @vport: vport to modify.
374 * @ids: new configuration, an array of port ids.
375 *
376 * Sets the vport's upcall_portids to @ids.
377 *
378 * Returns 0 if successful, -EINVAL if @ids is zero length or cannot be parsed
379 * as an array of U32.
380 *
381 * Must be called with ovs_mutex.
382 */
Thomas Graf12eb18f2014-11-06 06:58:52 -0800383int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids)
Alex Wang5cd667b2014-07-17 15:14:13 -0700384{
385 struct vport_portids *old, *vport_portids;
386
387 if (!nla_len(ids) || nla_len(ids) % sizeof(u32))
388 return -EINVAL;
389
390 old = ovsl_dereference(vport->upcall_portids);
391
392 vport_portids = kmalloc(sizeof(*vport_portids) + nla_len(ids),
393 GFP_KERNEL);
394 if (!vport_portids)
395 return -ENOMEM;
396
397 vport_portids->n_ids = nla_len(ids) / sizeof(u32);
398 vport_portids->rn_ids = reciprocal_value(vport_portids->n_ids);
399 nla_memcpy(vport_portids->ids, ids, nla_len(ids));
400
401 rcu_assign_pointer(vport->upcall_portids, vport_portids);
402
403 if (old)
404 kfree_rcu(old, rcu);
405 return 0;
406}
407
408/**
409 * ovs_vport_get_upcall_portids - get the upcall_portids of @vport.
410 *
411 * @vport: vport from which to retrieve the portids.
412 * @skb: sk_buff where portids should be appended.
413 *
414 * Retrieves the configuration of the given vport, appending the
415 * %OVS_VPORT_ATTR_UPCALL_PID attribute which is the array of upcall
416 * portids to @skb.
417 *
418 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room.
419 * If an error occurs, @skb is left unmodified. Must be called with
420 * ovs_mutex or rcu_read_lock.
421 */
422int ovs_vport_get_upcall_portids(const struct vport *vport,
423 struct sk_buff *skb)
424{
425 struct vport_portids *ids;
426
427 ids = rcu_dereference_ovsl(vport->upcall_portids);
428
429 if (vport->dp->user_features & OVS_DP_F_VPORT_PIDS)
430 return nla_put(skb, OVS_VPORT_ATTR_UPCALL_PID,
431 ids->n_ids * sizeof(u32), (void *)ids->ids);
432 else
433 return nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, ids->ids[0]);
434}
435
436/**
437 * ovs_vport_find_upcall_portid - find the upcall portid to send upcall.
438 *
439 * @vport: vport from which the missed packet is received.
440 * @skb: skb that the missed packet was received.
441 *
442 * Uses the skb_get_hash() to select the upcall portid to send the
443 * upcall.
444 *
445 * Returns the portid of the target socket. Must be called with rcu_read_lock.
446 */
Fabian Frederick4e8febd2014-10-15 21:03:41 +0200447u32 ovs_vport_find_upcall_portid(const struct vport *vport, struct sk_buff *skb)
Alex Wang5cd667b2014-07-17 15:14:13 -0700448{
449 struct vport_portids *ids;
450 u32 ids_index;
451 u32 hash;
452
Fabian Frederick4e8febd2014-10-15 21:03:41 +0200453 ids = rcu_dereference(vport->upcall_portids);
Alex Wang5cd667b2014-07-17 15:14:13 -0700454
455 if (ids->n_ids == 1 && ids->ids[0] == 0)
456 return 0;
457
458 hash = skb_get_hash(skb);
459 ids_index = hash - ids->n_ids * reciprocal_divide(hash, ids->rn_ids);
460 return ids->ids[ids_index];
461}
462
463/**
Jesse Grossccb13522011-10-25 19:26:31 -0700464 * ovs_vport_receive - pass up received packet to the datapath for processing
465 *
466 * @vport: vport that received the packet
467 * @skb: skb that was received
Justin Pettit26948382013-08-19 17:49:29 -0700468 * @tun_key: tunnel (if any) that carried packet
Jesse Grossccb13522011-10-25 19:26:31 -0700469 *
470 * Must be called with rcu_read_lock. The packet cannot be shared and
Cong Wangd176ca22013-02-22 19:41:26 +0800471 * skb->data should point to the Ethernet header.
Jesse Grossccb13522011-10-25 19:26:31 -0700472 */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700473void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
Thomas Graf12eb18f2014-11-06 06:58:52 -0800474 const struct ovs_tunnel_info *tun_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700475{
Li RongQing8f849852014-01-04 13:57:59 +0800476 struct pcpu_sw_netstats *stats;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700477 struct sw_flow_key key;
478 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700479
Shan Wei404f2f12012-11-13 09:52:25 +0800480 stats = this_cpu_ptr(vport->percpu_stats);
Pravin B Shelare0f0ecf2013-04-15 13:30:37 -0700481 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700482 stats->rx_packets++;
483 stats->rx_bytes += skb->len;
Pravin B Shelare0f0ecf2013-04-15 13:30:37 -0700484 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700485
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700486 OVS_CB(skb)->input_vport = vport;
Jesse Grossf0b128c2014-10-03 15:35:31 -0700487 OVS_CB(skb)->egress_tun_info = NULL;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700488 /* Extract flow from 'skb' into 'key'. */
Jesse Grossf0b128c2014-10-03 15:35:31 -0700489 error = ovs_flow_key_extract(tun_info, skb, &key);
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700490 if (unlikely(error)) {
491 kfree_skb(skb);
492 return;
493 }
494 ovs_dp_process_packet(skb, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700495}
Pravin B Shelar9ba559d2014-11-06 06:44:27 -0800496EXPORT_SYMBOL_GPL(ovs_vport_receive);
Jesse Grossccb13522011-10-25 19:26:31 -0700497
498/**
499 * ovs_vport_send - send a packet on a device
500 *
501 * @vport: vport on which to send the packet
502 * @skb: skb to send
503 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700504 * Sends the given packet and returns the length of data sent. Either ovs
Jesse Grossccb13522011-10-25 19:26:31 -0700505 * lock or rcu_read_lock must be held.
506 */
507int ovs_vport_send(struct vport *vport, struct sk_buff *skb)
508{
509 int sent = vport->ops->send(vport, skb);
510
Pravin B Shelar91b75142013-05-13 08:22:34 -0700511 if (likely(sent > 0)) {
Li RongQing8f849852014-01-04 13:57:59 +0800512 struct pcpu_sw_netstats *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700513
Shan Wei404f2f12012-11-13 09:52:25 +0800514 stats = this_cpu_ptr(vport->percpu_stats);
Jesse Grossccb13522011-10-25 19:26:31 -0700515
Pravin B Shelare0f0ecf2013-04-15 13:30:37 -0700516 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700517 stats->tx_packets++;
518 stats->tx_bytes += sent;
Pravin B Shelare0f0ecf2013-04-15 13:30:37 -0700519 u64_stats_update_end(&stats->syncp);
Pravin B Shelar91b75142013-05-13 08:22:34 -0700520 } else if (sent < 0) {
521 ovs_vport_record_error(vport, VPORT_E_TX_ERROR);
522 kfree_skb(skb);
523 } else
524 ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
525
Jesse Grossccb13522011-10-25 19:26:31 -0700526 return sent;
527}
528
529/**
530 * ovs_vport_record_error - indicate device error to generic stats layer
531 *
532 * @vport: vport that encountered the error
533 * @err_type: one of enum vport_err_type types to indicate the error type
534 *
535 * If using the vport generic stats layer indicate that an error of the given
Andy Hillaf784162013-06-07 16:53:50 -0700536 * type has occurred.
Jesse Grossccb13522011-10-25 19:26:31 -0700537 */
Stephen Hemminger443cd882013-12-17 19:22:48 +0000538static void ovs_vport_record_error(struct vport *vport,
539 enum vport_err_type err_type)
Jesse Grossccb13522011-10-25 19:26:31 -0700540{
Jesse Grossccb13522011-10-25 19:26:31 -0700541 switch (err_type) {
542 case VPORT_E_RX_DROPPED:
Li RongQinge403ade2014-09-06 19:06:11 +0800543 atomic_long_inc(&vport->err_stats.rx_dropped);
Jesse Grossccb13522011-10-25 19:26:31 -0700544 break;
545
546 case VPORT_E_RX_ERROR:
Li RongQinge403ade2014-09-06 19:06:11 +0800547 atomic_long_inc(&vport->err_stats.rx_errors);
Jesse Grossccb13522011-10-25 19:26:31 -0700548 break;
549
550 case VPORT_E_TX_DROPPED:
Li RongQinge403ade2014-09-06 19:06:11 +0800551 atomic_long_inc(&vport->err_stats.tx_dropped);
Jesse Grossccb13522011-10-25 19:26:31 -0700552 break;
553
554 case VPORT_E_TX_ERROR:
Li RongQinge403ade2014-09-06 19:06:11 +0800555 atomic_long_inc(&vport->err_stats.tx_errors);
Jesse Grossccb13522011-10-25 19:26:31 -0700556 break;
Peter Senna Tschudina2bf91b2012-09-18 07:10:44 +0000557 }
Jesse Grossccb13522011-10-25 19:26:31 -0700558
Jesse Grossccb13522011-10-25 19:26:31 -0700559}
Pravin B Shelaraa310702013-06-17 17:50:33 -0700560
561static void free_vport_rcu(struct rcu_head *rcu)
562{
563 struct vport *vport = container_of(rcu, struct vport, rcu);
564
565 ovs_vport_free(vport);
566}
567
568void ovs_vport_deferred_free(struct vport *vport)
569{
570 if (!vport)
571 return;
572
573 call_rcu(&vport->rcu, free_vport_rcu);
574}
Pravin B Shelar9ba559d2014-11-06 06:44:27 -0800575EXPORT_SYMBOL_GPL(ovs_vport_deferred_free);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800576
577int ovs_tunnel_get_egress_info(struct ovs_tunnel_info *egress_tun_info,
578 struct net *net,
579 const struct ovs_tunnel_info *tun_info,
580 u8 ipproto,
581 u32 skb_mark,
582 __be16 tp_src,
583 __be16 tp_dst)
584{
585 const struct ovs_key_ipv4_tunnel *tun_key;
586 struct rtable *rt;
587 struct flowi4 fl;
588
589 if (unlikely(!tun_info))
590 return -EINVAL;
591
592 tun_key = &tun_info->tunnel;
593
594 /* Route lookup to get srouce IP address.
595 * The process may need to be changed if the corresponding process
596 * in vports ops changed.
597 */
598 memset(&fl, 0, sizeof(fl));
599 fl.daddr = tun_key->ipv4_dst;
600 fl.saddr = tun_key->ipv4_src;
601 fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos);
602 fl.flowi4_mark = skb_mark;
Jiri Bencd2b2a132014-12-05 17:24:28 +0100603 fl.flowi4_proto = ipproto;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800604
605 rt = ip_route_output_key(net, &fl);
606 if (IS_ERR(rt))
607 return PTR_ERR(rt);
608
609 ip_rt_put(rt);
610
611 /* Generate egress_tun_info based on tun_info,
612 * saddr, tp_src and tp_dst
613 */
614 __ovs_flow_tun_info_init(egress_tun_info,
615 fl.saddr, tun_key->ipv4_dst,
616 tun_key->ipv4_tos,
617 tun_key->ipv4_ttl,
618 tp_src, tp_dst,
619 tun_key->tun_id,
620 tun_key->tun_flags,
621 tun_info->options,
622 tun_info->options_len);
623
624 return 0;
625}
626EXPORT_SYMBOL_GPL(ovs_tunnel_get_egress_info);
627
628int ovs_vport_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
629 struct ovs_tunnel_info *info)
630{
631 /* get_egress_tun_info() is only implemented on tunnel ports. */
632 if (unlikely(!vport->ops->get_egress_tun_info))
633 return -EINVAL;
634
635 return vport->ops->get_egress_tun_info(vport, skb, info);
636}