1c9422999SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ccb1352eSJesse Gross /*
383c8df26SPravin B Shelar * Copyright (c) 2007-2014 Nicira, Inc.
4ccb1352eSJesse Gross */
5ccb1352eSJesse Gross
6ccb1352eSJesse Gross #include <linux/etherdevice.h>
7ccb1352eSJesse Gross #include <linux/if.h>
8ccb1352eSJesse Gross #include <linux/if_vlan.h>
946df7b81SPravin B Shelar #include <linux/jhash.h>
10ccb1352eSJesse Gross #include <linux/kernel.h>
11ccb1352eSJesse Gross #include <linux/list.h>
12ccb1352eSJesse Gross #include <linux/mutex.h>
13ccb1352eSJesse Gross #include <linux/percpu.h>
14ccb1352eSJesse Gross #include <linux/rcupdate.h>
15ccb1352eSJesse Gross #include <linux/rtnetlink.h>
16ccb1352eSJesse Gross #include <linux/compat.h>
1746df7b81SPravin B Shelar #include <net/net_namespace.h>
1862b9c8d0SThomas Graf #include <linux/module.h>
19ccb1352eSJesse Gross
2046df7b81SPravin B Shelar #include "datapath.h"
21ccb1352eSJesse Gross #include "vport.h"
22ccb1352eSJesse Gross #include "vport-internal_dev.h"
23ccb1352eSJesse Gross
2462b9c8d0SThomas Graf static LIST_HEAD(vport_ops_list);
25ccb1352eSJesse Gross
268e4e1713SPravin B Shelar /* Protected by RCU read lock for reading, ovs_mutex for writing. */
27ccb1352eSJesse Gross static struct hlist_head *dev_table;
28ccb1352eSJesse Gross #define VPORT_HASH_BUCKETS 1024
29ccb1352eSJesse Gross
30ccb1352eSJesse Gross /**
31ccb1352eSJesse Gross * ovs_vport_init - initialize vport subsystem
32ccb1352eSJesse Gross *
33ccb1352eSJesse Gross * Called at module load time to initialize the vport subsystem.
34ccb1352eSJesse Gross */
ovs_vport_init(void)35ccb1352eSJesse Gross int ovs_vport_init(void)
36ccb1352eSJesse Gross {
376396bb22SKees Cook dev_table = kcalloc(VPORT_HASH_BUCKETS, sizeof(struct hlist_head),
38ccb1352eSJesse Gross GFP_KERNEL);
39ccb1352eSJesse Gross if (!dev_table)
40ccb1352eSJesse Gross return -ENOMEM;
41ccb1352eSJesse Gross
42ccb1352eSJesse Gross return 0;
43ccb1352eSJesse Gross }
44ccb1352eSJesse Gross
45ccb1352eSJesse Gross /**
46ccb1352eSJesse Gross * ovs_vport_exit - shutdown vport subsystem
47ccb1352eSJesse Gross *
48ccb1352eSJesse Gross * Called at module exit time to shutdown the vport subsystem.
49ccb1352eSJesse Gross */
ovs_vport_exit(void)50ccb1352eSJesse Gross void ovs_vport_exit(void)
51ccb1352eSJesse Gross {
52ccb1352eSJesse Gross kfree(dev_table);
53ccb1352eSJesse Gross }
54ccb1352eSJesse Gross
hash_bucket(const struct net * net,const char * name)5512eb18f7SThomas Graf static struct hlist_head *hash_bucket(const struct net *net, const char *name)
56ccb1352eSJesse Gross {
5746df7b81SPravin B Shelar unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
58ccb1352eSJesse Gross return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
59ccb1352eSJesse Gross }
60ccb1352eSJesse Gross
__ovs_vport_ops_register(struct vport_ops * ops)6183e4bf7aSPaolo Abeni int __ovs_vport_ops_register(struct vport_ops *ops)
6262b9c8d0SThomas Graf {
6362b9c8d0SThomas Graf int err = -EEXIST;
6462b9c8d0SThomas Graf struct vport_ops *o;
6562b9c8d0SThomas Graf
6662b9c8d0SThomas Graf ovs_lock();
6762b9c8d0SThomas Graf list_for_each_entry(o, &vport_ops_list, list)
6862b9c8d0SThomas Graf if (ops->type == o->type)
6962b9c8d0SThomas Graf goto errout;
7062b9c8d0SThomas Graf
7162b9c8d0SThomas Graf list_add_tail(&ops->list, &vport_ops_list);
7262b9c8d0SThomas Graf err = 0;
7362b9c8d0SThomas Graf errout:
7462b9c8d0SThomas Graf ovs_unlock();
7562b9c8d0SThomas Graf return err;
7662b9c8d0SThomas Graf }
7783e4bf7aSPaolo Abeni EXPORT_SYMBOL_GPL(__ovs_vport_ops_register);
7862b9c8d0SThomas Graf
ovs_vport_ops_unregister(struct vport_ops * ops)7962b9c8d0SThomas Graf void ovs_vport_ops_unregister(struct vport_ops *ops)
8062b9c8d0SThomas Graf {
8162b9c8d0SThomas Graf ovs_lock();
8262b9c8d0SThomas Graf list_del(&ops->list);
8362b9c8d0SThomas Graf ovs_unlock();
8462b9c8d0SThomas Graf }
859ba559d9SPravin B Shelar EXPORT_SYMBOL_GPL(ovs_vport_ops_unregister);
8662b9c8d0SThomas Graf
87ccb1352eSJesse Gross /**
88ccb1352eSJesse Gross * ovs_vport_locate - find a port that has already been created
89ccb1352eSJesse Gross *
9096678514SAndrew Lunn * @net: network namespace
91ccb1352eSJesse Gross * @name: name of port to find
92ccb1352eSJesse Gross *
938e4e1713SPravin B Shelar * Must be called with ovs or RCU read lock.
94ccb1352eSJesse Gross */
ovs_vport_locate(const struct net * net,const char * name)9512eb18f7SThomas Graf struct vport *ovs_vport_locate(const struct net *net, const char *name)
96ccb1352eSJesse Gross {
9746df7b81SPravin B Shelar struct hlist_head *bucket = hash_bucket(net, name);
98ccb1352eSJesse Gross struct vport *vport;
99ccb1352eSJesse Gross
100fed48423SMadhuparna Bhowmik hlist_for_each_entry_rcu(vport, bucket, hash_node,
101fed48423SMadhuparna Bhowmik lockdep_ovsl_is_held())
102c9db965cSThomas Graf if (!strcmp(name, ovs_vport_name(vport)) &&
10346df7b81SPravin B Shelar net_eq(ovs_dp_get_net(vport->dp), net))
104ccb1352eSJesse Gross return vport;
105ccb1352eSJesse Gross
106ccb1352eSJesse Gross return NULL;
107ccb1352eSJesse Gross }
108ccb1352eSJesse Gross
109ccb1352eSJesse Gross /**
110ccb1352eSJesse Gross * ovs_vport_alloc - allocate and initialize new vport
111ccb1352eSJesse Gross *
112ccb1352eSJesse Gross * @priv_size: Size of private data area to allocate.
113ccb1352eSJesse Gross * @ops: vport device ops
114210bba67SAndrew Lunn * @parms: information about new vport.
115ccb1352eSJesse Gross *
116ccb1352eSJesse Gross * Allocate and initialize a new vport defined by @ops. The vport will contain
117ccb1352eSJesse Gross * a private data area of size @priv_size that can be accessed using
118210bba67SAndrew Lunn * vport_priv(). Some parameters of the vport will be initialized from @parms.
119210bba67SAndrew Lunn * @vports that are no longer needed should be released with
120ccb1352eSJesse Gross * vport_free().
121ccb1352eSJesse Gross */
ovs_vport_alloc(int priv_size,const struct vport_ops * ops,const struct vport_parms * parms)122ccb1352eSJesse Gross struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
123ccb1352eSJesse Gross const struct vport_parms *parms)
124ccb1352eSJesse Gross {
125ccb1352eSJesse Gross struct vport *vport;
126ccb1352eSJesse Gross size_t alloc_size;
127*de9df6c6SEelco Chaudron int err;
128ccb1352eSJesse Gross
129ccb1352eSJesse Gross alloc_size = sizeof(struct vport);
130ccb1352eSJesse Gross if (priv_size) {
131ccb1352eSJesse Gross alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
132ccb1352eSJesse Gross alloc_size += priv_size;
133ccb1352eSJesse Gross }
134ccb1352eSJesse Gross
135ccb1352eSJesse Gross vport = kzalloc(alloc_size, GFP_KERNEL);
136ccb1352eSJesse Gross if (!vport)
137ccb1352eSJesse Gross return ERR_PTR(-ENOMEM);
138ccb1352eSJesse Gross
139*de9df6c6SEelco Chaudron vport->upcall_stats = netdev_alloc_pcpu_stats(struct vport_upcall_stats_percpu);
140*de9df6c6SEelco Chaudron if (!vport->upcall_stats) {
141*de9df6c6SEelco Chaudron err = -ENOMEM;
142*de9df6c6SEelco Chaudron goto err_kfree_vport;
143*de9df6c6SEelco Chaudron }
144*de9df6c6SEelco Chaudron
145ccb1352eSJesse Gross vport->dp = parms->dp;
146ccb1352eSJesse Gross vport->port_no = parms->port_no;
147ccb1352eSJesse Gross vport->ops = ops;
14815eac2a7SPravin B Shelar INIT_HLIST_NODE(&vport->dp_hash_node);
149ccb1352eSJesse Gross
1503791b3f6SChristoph Jaeger if (ovs_vport_set_upcall_portids(vport, parms->upcall_portids)) {
151*de9df6c6SEelco Chaudron err = -EINVAL;
152*de9df6c6SEelco Chaudron goto err_free_percpu;
1533791b3f6SChristoph Jaeger }
1545cd667b0SAlex Wang
155ccb1352eSJesse Gross return vport;
156*de9df6c6SEelco Chaudron
157*de9df6c6SEelco Chaudron err_free_percpu:
158*de9df6c6SEelco Chaudron free_percpu(vport->upcall_stats);
159*de9df6c6SEelco Chaudron err_kfree_vport:
160*de9df6c6SEelco Chaudron kfree(vport);
161*de9df6c6SEelco Chaudron return ERR_PTR(err);
162ccb1352eSJesse Gross }
1639ba559d9SPravin B Shelar EXPORT_SYMBOL_GPL(ovs_vport_alloc);
164ccb1352eSJesse Gross
165ccb1352eSJesse Gross /**
166ccb1352eSJesse Gross * ovs_vport_free - uninitialize and free vport
167ccb1352eSJesse Gross *
168ccb1352eSJesse Gross * @vport: vport to free
169ccb1352eSJesse Gross *
170ccb1352eSJesse Gross * Frees a vport allocated with vport_alloc() when it is no longer needed.
171ccb1352eSJesse Gross *
172ccb1352eSJesse Gross * The caller must ensure that an RCU grace period has passed since the last
173ccb1352eSJesse Gross * time @vport was in a datapath.
174ccb1352eSJesse Gross */
ovs_vport_free(struct vport * vport)175ccb1352eSJesse Gross void ovs_vport_free(struct vport *vport)
176ccb1352eSJesse Gross {
1775cd667b0SAlex Wang /* vport is freed from RCU callback or error path, Therefore
1785cd667b0SAlex Wang * it is safe to use raw dereference.
1795cd667b0SAlex Wang */
1805cd667b0SAlex Wang kfree(rcu_dereference_raw(vport->upcall_portids));
181*de9df6c6SEelco Chaudron free_percpu(vport->upcall_stats);
182ccb1352eSJesse Gross kfree(vport);
183ccb1352eSJesse Gross }
1849ba559d9SPravin B Shelar EXPORT_SYMBOL_GPL(ovs_vport_free);
18562b9c8d0SThomas Graf
ovs_vport_lookup(const struct vport_parms * parms)18662b9c8d0SThomas Graf static struct vport_ops *ovs_vport_lookup(const struct vport_parms *parms)
18762b9c8d0SThomas Graf {
18862b9c8d0SThomas Graf struct vport_ops *ops;
18962b9c8d0SThomas Graf
19062b9c8d0SThomas Graf list_for_each_entry(ops, &vport_ops_list, list)
19162b9c8d0SThomas Graf if (ops->type == parms->type)
19262b9c8d0SThomas Graf return ops;
19362b9c8d0SThomas Graf
19462b9c8d0SThomas Graf return NULL;
19562b9c8d0SThomas Graf }
196ccb1352eSJesse Gross
197ccb1352eSJesse Gross /**
198ccb1352eSJesse Gross * ovs_vport_add - add vport device (for kernel callers)
199ccb1352eSJesse Gross *
200ccb1352eSJesse Gross * @parms: Information about new vport.
201ccb1352eSJesse Gross *
202ccb1352eSJesse Gross * Creates a new vport with the specified configuration (which is dependent on
2038e4e1713SPravin B Shelar * device type). ovs_mutex must be held.
204ccb1352eSJesse Gross */
ovs_vport_add(const struct vport_parms * parms)205ccb1352eSJesse Gross struct vport *ovs_vport_add(const struct vport_parms *parms)
206ccb1352eSJesse Gross {
20762b9c8d0SThomas Graf struct vport_ops *ops;
208ccb1352eSJesse Gross struct vport *vport;
209ccb1352eSJesse Gross
21062b9c8d0SThomas Graf ops = ovs_vport_lookup(parms);
21162b9c8d0SThomas Graf if (ops) {
21246df7b81SPravin B Shelar struct hlist_head *bucket;
21346df7b81SPravin B Shelar
21462b9c8d0SThomas Graf if (!try_module_get(ops->owner))
21562b9c8d0SThomas Graf return ERR_PTR(-EAFNOSUPPORT);
21662b9c8d0SThomas Graf
21762b9c8d0SThomas Graf vport = ops->create(parms);
218ccb1352eSJesse Gross if (IS_ERR(vport)) {
21962b9c8d0SThomas Graf module_put(ops->owner);
22062b9c8d0SThomas Graf return vport;
221ccb1352eSJesse Gross }
222ccb1352eSJesse Gross
22346df7b81SPravin B Shelar bucket = hash_bucket(ovs_dp_get_net(vport->dp),
224c9db965cSThomas Graf ovs_vport_name(vport));
22546df7b81SPravin B Shelar hlist_add_head_rcu(&vport->hash_node, bucket);
226ccb1352eSJesse Gross return vport;
227ccb1352eSJesse Gross }
228ccb1352eSJesse Gross
22962b9c8d0SThomas Graf /* Unlock to attempt module load and return -EAGAIN if load
23062b9c8d0SThomas Graf * was successful as we need to restart the port addition
23162b9c8d0SThomas Graf * workflow.
23262b9c8d0SThomas Graf */
23362b9c8d0SThomas Graf ovs_unlock();
23462b9c8d0SThomas Graf request_module("vport-type-%d", parms->type);
23562b9c8d0SThomas Graf ovs_lock();
236ccb1352eSJesse Gross
23762b9c8d0SThomas Graf if (!ovs_vport_lookup(parms))
23862b9c8d0SThomas Graf return ERR_PTR(-EAFNOSUPPORT);
23962b9c8d0SThomas Graf else
24062b9c8d0SThomas Graf return ERR_PTR(-EAGAIN);
241ccb1352eSJesse Gross }
242ccb1352eSJesse Gross
243ccb1352eSJesse Gross /**
244ccb1352eSJesse Gross * ovs_vport_set_options - modify existing vport device (for kernel callers)
245ccb1352eSJesse Gross *
246ccb1352eSJesse Gross * @vport: vport to modify.
2472694838dSJustin Pettit * @options: New configuration.
248ccb1352eSJesse Gross *
249ccb1352eSJesse Gross * Modifies an existing device with the specified configuration (which is
2508e4e1713SPravin B Shelar * dependent on device type). ovs_mutex must be held.
251ccb1352eSJesse Gross */
ovs_vport_set_options(struct vport * vport,struct nlattr * options)252ccb1352eSJesse Gross int ovs_vport_set_options(struct vport *vport, struct nlattr *options)
253ccb1352eSJesse Gross {
254ccb1352eSJesse Gross if (!vport->ops->set_options)
255ccb1352eSJesse Gross return -EOPNOTSUPP;
256ccb1352eSJesse Gross return vport->ops->set_options(vport, options);
257ccb1352eSJesse Gross }
258ccb1352eSJesse Gross
259ccb1352eSJesse Gross /**
260ccb1352eSJesse Gross * ovs_vport_del - delete existing vport device
261ccb1352eSJesse Gross *
262ccb1352eSJesse Gross * @vport: vport to delete.
263ccb1352eSJesse Gross *
26420f79566SAaron Conole * Detaches @vport from its datapath and destroys it. ovs_mutex must
26520f79566SAaron Conole * be held.
266ccb1352eSJesse Gross */
ovs_vport_del(struct vport * vport)267ccb1352eSJesse Gross void ovs_vport_del(struct vport *vport)
268ccb1352eSJesse Gross {
269ccb1352eSJesse Gross hlist_del_rcu(&vport->hash_node);
27062b9c8d0SThomas Graf module_put(vport->ops->owner);
271fa2d8ff4SThomas Graf vport->ops->destroy(vport);
272ccb1352eSJesse Gross }
273ccb1352eSJesse Gross
274ccb1352eSJesse Gross /**
275ccb1352eSJesse Gross * ovs_vport_get_stats - retrieve device stats
276ccb1352eSJesse Gross *
277ccb1352eSJesse Gross * @vport: vport from which to retrieve the stats
278ccb1352eSJesse Gross * @stats: location to store stats
279ccb1352eSJesse Gross *
280ccb1352eSJesse Gross * Retrieves transmit, receive, and error stats for the given device.
281ccb1352eSJesse Gross *
2828e4e1713SPravin B Shelar * Must be called with ovs_mutex or rcu_read_lock.
283ccb1352eSJesse Gross */
ovs_vport_get_stats(struct vport * vport,struct ovs_vport_stats * stats)284ccb1352eSJesse Gross void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
285ccb1352eSJesse Gross {
28683ffe99fSPravin B Shelar const struct rtnl_link_stats64 *dev_stats;
28783ffe99fSPravin B Shelar struct rtnl_link_stats64 temp;
288ccb1352eSJesse Gross
28983ffe99fSPravin B Shelar dev_stats = dev_get_stats(vport->dev, &temp);
29083ffe99fSPravin B Shelar stats->rx_errors = dev_stats->rx_errors;
29183ffe99fSPravin B Shelar stats->tx_errors = dev_stats->tx_errors;
29283ffe99fSPravin B Shelar stats->tx_dropped = dev_stats->tx_dropped;
29383ffe99fSPravin B Shelar stats->rx_dropped = dev_stats->rx_dropped;
294ccb1352eSJesse Gross
29583ffe99fSPravin B Shelar stats->rx_bytes = dev_stats->rx_bytes;
29683ffe99fSPravin B Shelar stats->rx_packets = dev_stats->rx_packets;
29783ffe99fSPravin B Shelar stats->tx_bytes = dev_stats->tx_bytes;
29883ffe99fSPravin B Shelar stats->tx_packets = dev_stats->tx_packets;
299ccb1352eSJesse Gross }
300ccb1352eSJesse Gross
301ccb1352eSJesse Gross /**
3021933ea36Swangchuanlei * ovs_vport_get_upcall_stats - retrieve upcall stats
3031933ea36Swangchuanlei *
3041933ea36Swangchuanlei * @vport: vport from which to retrieve the stats.
3051933ea36Swangchuanlei * @skb: sk_buff where upcall stats should be appended.
3061933ea36Swangchuanlei *
3071933ea36Swangchuanlei * Retrieves upcall stats for the given device.
3081933ea36Swangchuanlei *
3091933ea36Swangchuanlei * Must be called with ovs_mutex or rcu_read_lock.
3101933ea36Swangchuanlei */
ovs_vport_get_upcall_stats(struct vport * vport,struct sk_buff * skb)3111933ea36Swangchuanlei int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb)
3121933ea36Swangchuanlei {
3131933ea36Swangchuanlei struct nlattr *nla;
3141933ea36Swangchuanlei int i;
3151933ea36Swangchuanlei
3161933ea36Swangchuanlei __u64 tx_success = 0;
3171933ea36Swangchuanlei __u64 tx_fail = 0;
3181933ea36Swangchuanlei
3191933ea36Swangchuanlei for_each_possible_cpu(i) {
3201933ea36Swangchuanlei const struct vport_upcall_stats_percpu *stats;
3211933ea36Swangchuanlei unsigned int start;
3221933ea36Swangchuanlei
3231933ea36Swangchuanlei stats = per_cpu_ptr(vport->upcall_stats, i);
3241933ea36Swangchuanlei do {
3251933ea36Swangchuanlei start = u64_stats_fetch_begin(&stats->syncp);
3261933ea36Swangchuanlei tx_success += u64_stats_read(&stats->n_success);
3271933ea36Swangchuanlei tx_fail += u64_stats_read(&stats->n_fail);
3281933ea36Swangchuanlei } while (u64_stats_fetch_retry(&stats->syncp, start));
3291933ea36Swangchuanlei }
3301933ea36Swangchuanlei
3311933ea36Swangchuanlei nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_UPCALL_STATS);
3321933ea36Swangchuanlei if (!nla)
3331933ea36Swangchuanlei return -EMSGSIZE;
3341933ea36Swangchuanlei
3351933ea36Swangchuanlei if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_SUCCESS, tx_success,
3361933ea36Swangchuanlei OVS_VPORT_ATTR_PAD)) {
3371933ea36Swangchuanlei nla_nest_cancel(skb, nla);
3381933ea36Swangchuanlei return -EMSGSIZE;
3391933ea36Swangchuanlei }
3401933ea36Swangchuanlei
3411933ea36Swangchuanlei if (nla_put_u64_64bit(skb, OVS_VPORT_UPCALL_ATTR_FAIL, tx_fail,
3421933ea36Swangchuanlei OVS_VPORT_ATTR_PAD)) {
3431933ea36Swangchuanlei nla_nest_cancel(skb, nla);
3441933ea36Swangchuanlei return -EMSGSIZE;
3451933ea36Swangchuanlei }
3461933ea36Swangchuanlei nla_nest_end(skb, nla);
3471933ea36Swangchuanlei
3481933ea36Swangchuanlei return 0;
3491933ea36Swangchuanlei }
3501933ea36Swangchuanlei
3511933ea36Swangchuanlei /**
352ccb1352eSJesse Gross * ovs_vport_get_options - retrieve device options
353ccb1352eSJesse Gross *
354ccb1352eSJesse Gross * @vport: vport from which to retrieve the options.
355ccb1352eSJesse Gross * @skb: sk_buff where options should be appended.
356ccb1352eSJesse Gross *
357ccb1352eSJesse Gross * Retrieves the configuration of the given device, appending an
358ccb1352eSJesse Gross * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
359ccb1352eSJesse Gross * vport-specific attributes to @skb.
360ccb1352eSJesse Gross *
361ccb1352eSJesse Gross * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
362ccb1352eSJesse Gross * negative error code if a real error occurred. If an error occurs, @skb is
363ccb1352eSJesse Gross * left unmodified.
364ccb1352eSJesse Gross *
3658e4e1713SPravin B Shelar * Must be called with ovs_mutex or rcu_read_lock.
366ccb1352eSJesse Gross */
ovs_vport_get_options(const struct vport * vport,struct sk_buff * skb)367ccb1352eSJesse Gross int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
368ccb1352eSJesse Gross {
369ccb1352eSJesse Gross struct nlattr *nla;
3705d963352SThomas Graf int err;
3715d963352SThomas Graf
3725d963352SThomas Graf if (!vport->ops->get_options)
3735d963352SThomas Graf return 0;
374ccb1352eSJesse Gross
375ae0be8deSMichal Kubecek nla = nla_nest_start_noflag(skb, OVS_VPORT_ATTR_OPTIONS);
376ccb1352eSJesse Gross if (!nla)
377ccb1352eSJesse Gross return -EMSGSIZE;
378ccb1352eSJesse Gross
3795d963352SThomas Graf err = vport->ops->get_options(vport, skb);
380ccb1352eSJesse Gross if (err) {
381ccb1352eSJesse Gross nla_nest_cancel(skb, nla);
382ccb1352eSJesse Gross return err;
383ccb1352eSJesse Gross }
384ccb1352eSJesse Gross
385ccb1352eSJesse Gross nla_nest_end(skb, nla);
386ccb1352eSJesse Gross return 0;
387ccb1352eSJesse Gross }
388ccb1352eSJesse Gross
389ccb1352eSJesse Gross /**
3905cd667b0SAlex Wang * ovs_vport_set_upcall_portids - set upcall portids of @vport.
3915cd667b0SAlex Wang *
3925cd667b0SAlex Wang * @vport: vport to modify.
3935cd667b0SAlex Wang * @ids: new configuration, an array of port ids.
3945cd667b0SAlex Wang *
3955cd667b0SAlex Wang * Sets the vport's upcall_portids to @ids.
3965cd667b0SAlex Wang *
3975cd667b0SAlex Wang * Returns 0 if successful, -EINVAL if @ids is zero length or cannot be parsed
3985cd667b0SAlex Wang * as an array of U32.
3995cd667b0SAlex Wang *
4005cd667b0SAlex Wang * Must be called with ovs_mutex.
4015cd667b0SAlex Wang */
ovs_vport_set_upcall_portids(struct vport * vport,const struct nlattr * ids)40212eb18f7SThomas Graf int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids)
4035cd667b0SAlex Wang {
4045cd667b0SAlex Wang struct vport_portids *old, *vport_portids;
4055cd667b0SAlex Wang
4065cd667b0SAlex Wang if (!nla_len(ids) || nla_len(ids) % sizeof(u32))
4075cd667b0SAlex Wang return -EINVAL;
4085cd667b0SAlex Wang
4095cd667b0SAlex Wang old = ovsl_dereference(vport->upcall_portids);
4105cd667b0SAlex Wang
4115cd667b0SAlex Wang vport_portids = kmalloc(sizeof(*vport_portids) + nla_len(ids),
4125cd667b0SAlex Wang GFP_KERNEL);
4135cd667b0SAlex Wang if (!vport_portids)
4145cd667b0SAlex Wang return -ENOMEM;
4155cd667b0SAlex Wang
4165cd667b0SAlex Wang vport_portids->n_ids = nla_len(ids) / sizeof(u32);
4175cd667b0SAlex Wang vport_portids->rn_ids = reciprocal_value(vport_portids->n_ids);
4185cd667b0SAlex Wang nla_memcpy(vport_portids->ids, ids, nla_len(ids));
4195cd667b0SAlex Wang
4205cd667b0SAlex Wang rcu_assign_pointer(vport->upcall_portids, vport_portids);
4215cd667b0SAlex Wang
4225cd667b0SAlex Wang if (old)
4235cd667b0SAlex Wang kfree_rcu(old, rcu);
4245cd667b0SAlex Wang return 0;
4255cd667b0SAlex Wang }
4265cd667b0SAlex Wang
4275cd667b0SAlex Wang /**
4285cd667b0SAlex Wang * ovs_vport_get_upcall_portids - get the upcall_portids of @vport.
4295cd667b0SAlex Wang *
4305cd667b0SAlex Wang * @vport: vport from which to retrieve the portids.
4315cd667b0SAlex Wang * @skb: sk_buff where portids should be appended.
4325cd667b0SAlex Wang *
4335cd667b0SAlex Wang * Retrieves the configuration of the given vport, appending the
4345cd667b0SAlex Wang * %OVS_VPORT_ATTR_UPCALL_PID attribute which is the array of upcall
4355cd667b0SAlex Wang * portids to @skb.
4365cd667b0SAlex Wang *
4375cd667b0SAlex Wang * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room.
4385cd667b0SAlex Wang * If an error occurs, @skb is left unmodified. Must be called with
4395cd667b0SAlex Wang * ovs_mutex or rcu_read_lock.
4405cd667b0SAlex Wang */
ovs_vport_get_upcall_portids(const struct vport * vport,struct sk_buff * skb)4415cd667b0SAlex Wang int ovs_vport_get_upcall_portids(const struct vport *vport,
4425cd667b0SAlex Wang struct sk_buff *skb)
4435cd667b0SAlex Wang {
4445cd667b0SAlex Wang struct vport_portids *ids;
4455cd667b0SAlex Wang
4465cd667b0SAlex Wang ids = rcu_dereference_ovsl(vport->upcall_portids);
4475cd667b0SAlex Wang
4485cd667b0SAlex Wang if (vport->dp->user_features & OVS_DP_F_VPORT_PIDS)
4495cd667b0SAlex Wang return nla_put(skb, OVS_VPORT_ATTR_UPCALL_PID,
4505cd667b0SAlex Wang ids->n_ids * sizeof(u32), (void *)ids->ids);
4515cd667b0SAlex Wang else
4525cd667b0SAlex Wang return nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, ids->ids[0]);
4535cd667b0SAlex Wang }
4545cd667b0SAlex Wang
4555cd667b0SAlex Wang /**
4565cd667b0SAlex Wang * ovs_vport_find_upcall_portid - find the upcall portid to send upcall.
4575cd667b0SAlex Wang *
4585cd667b0SAlex Wang * @vport: vport from which the missed packet is received.
4595cd667b0SAlex Wang * @skb: skb that the missed packet was received.
4605cd667b0SAlex Wang *
4615cd667b0SAlex Wang * Uses the skb_get_hash() to select the upcall portid to send the
4625cd667b0SAlex Wang * upcall.
4635cd667b0SAlex Wang *
4645cd667b0SAlex Wang * Returns the portid of the target socket. Must be called with rcu_read_lock.
4655cd667b0SAlex Wang */
ovs_vport_find_upcall_portid(const struct vport * vport,struct sk_buff * skb)466cf3266adSTonghao Zhang u32 ovs_vport_find_upcall_portid(const struct vport *vport,
467cf3266adSTonghao Zhang struct sk_buff *skb)
4685cd667b0SAlex Wang {
4695cd667b0SAlex Wang struct vport_portids *ids;
4705cd667b0SAlex Wang u32 ids_index;
4715cd667b0SAlex Wang u32 hash;
4725cd667b0SAlex Wang
4734e8febd0SFabian Frederick ids = rcu_dereference(vport->upcall_portids);
4745cd667b0SAlex Wang
47590ce9f23STonghao Zhang /* If there is only one portid, select it in the fast-path. */
47690ce9f23STonghao Zhang if (ids->n_ids == 1)
47790ce9f23STonghao Zhang return ids->ids[0];
4785cd667b0SAlex Wang
4795cd667b0SAlex Wang hash = skb_get_hash(skb);
4805cd667b0SAlex Wang ids_index = hash - ids->n_ids * reciprocal_divide(hash, ids->rn_ids);
4815cd667b0SAlex Wang return ids->ids[ids_index];
4825cd667b0SAlex Wang }
4835cd667b0SAlex Wang
4845cd667b0SAlex Wang /**
485ccb1352eSJesse Gross * ovs_vport_receive - pass up received packet to the datapath for processing
486ccb1352eSJesse Gross *
487ccb1352eSJesse Gross * @vport: vport that received the packet
488ccb1352eSJesse Gross * @skb: skb that was received
48996678514SAndrew Lunn * @tun_info: tunnel (if any) that carried packet
490ccb1352eSJesse Gross *
491ccb1352eSJesse Gross * Must be called with rcu_read_lock. The packet cannot be shared and
492d176ca2aSCong Wang * skb->data should point to the Ethernet header.
493ccb1352eSJesse Gross */
ovs_vport_receive(struct vport * vport,struct sk_buff * skb,const struct ip_tunnel_info * tun_info)4948c876639SPravin B Shelar int ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
4951d8fff90SThomas Graf const struct ip_tunnel_info *tun_info)
496ccb1352eSJesse Gross {
4978c8b1b83SPravin B Shelar struct sw_flow_key key;
4988c8b1b83SPravin B Shelar int error;
499ccb1352eSJesse Gross
50083c8df26SPravin B Shelar OVS_CB(skb)->input_vport = vport;
5017f8a436eSJoe Stringer OVS_CB(skb)->mru = 0;
502f2a4d086SWilliam Tu OVS_CB(skb)->cutlen = 0;
503740dbc28SJoe Stringer if (unlikely(dev_net(skb->dev) != ovs_dp_get_net(vport->dp))) {
504740dbc28SJoe Stringer u32 mark;
505740dbc28SJoe Stringer
506740dbc28SJoe Stringer mark = skb->mark;
507740dbc28SJoe Stringer skb_scrub_packet(skb, true);
508740dbc28SJoe Stringer skb->mark = mark;
509740dbc28SJoe Stringer tun_info = NULL;
510740dbc28SJoe Stringer }
511740dbc28SJoe Stringer
5128c8b1b83SPravin B Shelar /* Extract flow from 'skb' into 'key'. */
513f0b128c1SJesse Gross error = ovs_flow_key_extract(tun_info, skb, &key);
5148c8b1b83SPravin B Shelar if (unlikely(error)) {
5158c8b1b83SPravin B Shelar kfree_skb(skb);
5168c876639SPravin B Shelar return error;
5178c8b1b83SPravin B Shelar }
5188c8b1b83SPravin B Shelar ovs_dp_process_packet(skb, &key);
5198c876639SPravin B Shelar return 0;
520ccb1352eSJesse Gross }
521ccb1352eSJesse Gross
packet_length(const struct sk_buff * skb,struct net_device * dev)52246e371f0SWilliam Tu static int packet_length(const struct sk_buff *skb,
523738314a0SJiri Benc struct net_device *dev)
524aec15924SPravin B Shelar {
52546e371f0SWilliam Tu int length = skb->len - dev->hard_header_len;
526aec15924SPravin B Shelar
52772ec108dSJiri Benc if (!skb_vlan_tag_present(skb) &&
52872ec108dSJiri Benc eth_type_vlan(skb->protocol))
529aec15924SPravin B Shelar length -= VLAN_HLEN;
530aec15924SPravin B Shelar
531018c1ddaSEric Garver /* Don't subtract for multiple VLAN tags. Most (all?) drivers allow
532018c1ddaSEric Garver * (ETH_LEN + VLAN_HLEN) in addition to the mtu value, but almost none
533018c1ddaSEric Garver * account for 802.1ad. e.g. is_skb_forwardable().
534018c1ddaSEric Garver */
535018c1ddaSEric Garver
53646e371f0SWilliam Tu return length > 0 ? length : 0;
537aec15924SPravin B Shelar }
538aec15924SPravin B Shelar
ovs_vport_send(struct vport * vport,struct sk_buff * skb,u8 mac_proto)539e2d9d835SJiri Benc void ovs_vport_send(struct vport *vport, struct sk_buff *skb, u8 mac_proto)
540aec15924SPravin B Shelar {
541aec15924SPravin B Shelar int mtu = vport->dev->mtu;
542aec15924SPravin B Shelar
5435108bbadSJiri Benc switch (vport->dev->type) {
5445108bbadSJiri Benc case ARPHRD_NONE:
5455108bbadSJiri Benc if (mac_proto == MAC_PROTO_ETHERNET) {
5465108bbadSJiri Benc skb_reset_network_header(skb);
5475108bbadSJiri Benc skb_reset_mac_len(skb);
5485108bbadSJiri Benc skb->protocol = htons(ETH_P_TEB);
5495108bbadSJiri Benc } else if (mac_proto != MAC_PROTO_NONE) {
5505108bbadSJiri Benc WARN_ON_ONCE(1);
5515108bbadSJiri Benc goto drop;
5525108bbadSJiri Benc }
5535108bbadSJiri Benc break;
5545108bbadSJiri Benc case ARPHRD_ETHER:
5555108bbadSJiri Benc if (mac_proto != MAC_PROTO_ETHERNET)
5565108bbadSJiri Benc goto drop;
5575108bbadSJiri Benc break;
5585108bbadSJiri Benc default:
5595108bbadSJiri Benc goto drop;
5605108bbadSJiri Benc }
5615108bbadSJiri Benc
562738314a0SJiri Benc if (unlikely(packet_length(skb, vport->dev) > mtu &&
563738314a0SJiri Benc !skb_is_gso(skb))) {
564aec15924SPravin B Shelar vport->dev->stats.tx_errors++;
565ebfbc46bSFlavio Leitner if (vport->dev->flags & IFF_UP)
566ebfbc46bSFlavio Leitner net_warn_ratelimited("%s: dropped over-mtu packet: "
567ebfbc46bSFlavio Leitner "%d > %d\n", vport->dev->name,
568ebfbc46bSFlavio Leitner packet_length(skb, vport->dev),
569ebfbc46bSFlavio Leitner mtu);
570aec15924SPravin B Shelar goto drop;
571aec15924SPravin B Shelar }
572aec15924SPravin B Shelar
573aec15924SPravin B Shelar skb->dev = vport->dev;
574de799101SMartin KaFai Lau skb_clear_tstamp(skb);
575aec15924SPravin B Shelar vport->ops->send(skb);
576aec15924SPravin B Shelar return;
577aec15924SPravin B Shelar
578aec15924SPravin B Shelar drop:
579aec15924SPravin B Shelar kfree_skb(skb);
580aec15924SPravin B Shelar }
581