vport.h revision b34df5e805a6e98cae0bc5bc80c1b52d9ff811de
1ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/*
2caf2ee14bbc2c6bd73cf0decf576007e0239a482Raju Subramanian * Copyright (c) 2007-2012 Nicira, Inc.
3ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
4ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * This program is free software; you can redistribute it and/or
5ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * modify it under the terms of version 2 of the GNU General Public
6ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * License as published by the Free Software Foundation.
7ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
8ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * This program is distributed in the hope that it will be useful, but
9ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * WITHOUT ANY WARRANTY; without even the implied warranty of
10ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * General Public License for more details.
12ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
13ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * You should have received a copy of the GNU General Public License
14ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * along with this program; if not, write to the Free Software
15ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * 02110-1301, USA
17ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross */
18ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
19ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#ifndef VPORT_H
20ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#define VPORT_H 1
21ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
22e0f0ecf33c3f13401f90bff5afdc3ed1bb40b9afPravin B Shelar#include <linux/if_tunnel.h>
23ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#include <linux/list.h>
2446df7b814548849deee01f50bc75f8f5ae8cd767Pravin B Shelar#include <linux/netlink.h>
25ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#include <linux/openvswitch.h>
26ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#include <linux/skbuff.h>
27ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#include <linux/spinlock.h>
28ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#include <linux/u64_stats_sync.h>
29ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
30ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#include "datapath.h"
31ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
32ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstruct vport;
33ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstruct vport_parms;
34ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
35ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/* The following definitions are for users of the vport subsytem: */
36ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
37ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossint ovs_vport_init(void);
38ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossvoid ovs_vport_exit(void);
39ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
40ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstruct vport *ovs_vport_add(const struct vport_parms *);
41ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossvoid ovs_vport_del(struct vport *);
42ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
4346df7b814548849deee01f50bc75f8f5ae8cd767Pravin B Shelarstruct vport *ovs_vport_locate(struct net *net, const char *name);
44ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
45ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossvoid ovs_vport_get_stats(struct vport *, struct ovs_vport_stats *);
46ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
47ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossint ovs_vport_set_options(struct vport *, struct nlattr *options);
48ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossint ovs_vport_get_options(const struct vport *, struct sk_buff *);
49ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
50ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossint ovs_vport_send(struct vport *, struct sk_buff *);
51ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
52ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/* The following definitions are for implementers of vport devices: */
53ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
54ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstruct vport_err_stats {
55ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	u64 rx_dropped;
56ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	u64 rx_errors;
57ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	u64 tx_dropped;
58ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	u64 tx_errors;
59ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross};
60ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
61ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/**
62ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * struct vport - one port within a datapath
63ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @rcu: RCU callback head for deferred destruction.
64ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @dp: Datapath to which this port belongs.
6515e473046cb6e5d18a4d0057e61d76315230382bEric W. Biederman * @upcall_portid: The Netlink port to use for packets received on this port that
66ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * miss the flow table.
6703599c94111bdac92fb5a70d592f5382b6fda75fThomas Graf * @port_no: Index into @dp's @ports array.
68ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @hash_node: Element in @dev_table hash table in vport.c.
6915eac2a74277bc7de68a7c2a64a7c91b4b6f5961Pravin B Shelar * @dp_hash_node: Element in @datapath->ports hash table in datapath.c.
70ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @ops: Class structure.
71ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @percpu_stats: Points to per-CPU statistics used and maintained by vport
72ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @stats_lock: Protects @err_stats;
73ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @err_stats: Points to error statistics used and maintained by vport
74ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross */
75ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstruct vport {
76ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	struct rcu_head rcu;
77ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	struct datapath	*dp;
7815e473046cb6e5d18a4d0057e61d76315230382bEric W. Biederman	u32 upcall_portid;
7903599c94111bdac92fb5a70d592f5382b6fda75fThomas Graf	u16 port_no;
80ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
81ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	struct hlist_node hash_node;
8215eac2a74277bc7de68a7c2a64a7c91b4b6f5961Pravin B Shelar	struct hlist_node dp_hash_node;
83ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	const struct vport_ops *ops;
84ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
85e0f0ecf33c3f13401f90bff5afdc3ed1bb40b9afPravin B Shelar	struct pcpu_tstats __percpu *percpu_stats;
86ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
87ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	spinlock_t stats_lock;
88ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	struct vport_err_stats err_stats;
89ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross};
90ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
91ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/**
92ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * struct vport_parms - parameters for creating a new vport
93ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
94ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @name: New vport's name.
95ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @type: New vport's type.
96ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @options: %OVS_VPORT_ATTR_OPTIONS attribute from Netlink message, %NULL if
97ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * none was supplied.
98ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @dp: New vport's datapath.
99ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @port_no: New vport's port number.
100ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross */
101ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstruct vport_parms {
102ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	const char *name;
103ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	enum ovs_vport_type type;
104ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	struct nlattr *options;
105ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
106ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	/* For ovs_vport_alloc(). */
107ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	struct datapath *dp;
108ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	u16 port_no;
10915e473046cb6e5d18a4d0057e61d76315230382bEric W. Biederman	u32 upcall_portid;
110ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross};
111ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
112ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/**
113ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * struct vport_ops - definition of a type of virtual port
114ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
115ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @type: %OVS_VPORT_TYPE_* value for this type of virtual port.
116ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @create: Create a new vport configured as specified.  On success returns
117ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * a new vport allocated with ovs_vport_alloc(), otherwise an ERR_PTR() value.
118ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @destroy: Destroys a vport.  Must call vport_free() on the vport but not
119ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * before an RCU grace period has elapsed.
120ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @set_options: Modify the configuration of an existing vport.  May be %NULL
121ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * if modification is not supported.
122ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @get_options: Appends vport-specific attributes for the configuration of an
123ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * existing vport to a &struct sk_buff.  May be %NULL for a vport that does not
124ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * have any configuration.
125ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @get_name: Get the device's name.
12691b7514cdff406ad8f63d09b74f664c37bed2e01Pravin B Shelar * @send: Send a packet on the device.  Returns the length of the packet sent,
12791b7514cdff406ad8f63d09b74f664c37bed2e01Pravin B Shelar * zero for dropped packets or negative for error.
128ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross */
129ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstruct vport_ops {
130ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	enum ovs_vport_type type;
131ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
1328e4e1713e4978447c5f799aa668dcc6d2cb0dee9Pravin B Shelar	/* Called with ovs_mutex. */
133ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	struct vport *(*create)(const struct vport_parms *);
134ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	void (*destroy)(struct vport *);
135ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
136ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	int (*set_options)(struct vport *, struct nlattr *);
137ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	int (*get_options)(const struct vport *, struct sk_buff *);
138ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
1398e4e1713e4978447c5f799aa668dcc6d2cb0dee9Pravin B Shelar	/* Called with rcu_read_lock or ovs_mutex. */
140ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	const char *(*get_name)(const struct vport *);
141ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
142ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	int (*send)(struct vport *, struct sk_buff *);
143ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross};
144ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
145ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossenum vport_err_type {
146ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	VPORT_E_RX_DROPPED,
147ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	VPORT_E_RX_ERROR,
148ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	VPORT_E_TX_DROPPED,
149ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	VPORT_E_TX_ERROR,
150ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross};
151ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
152ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstruct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *,
153ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross			      const struct vport_parms *);
154ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossvoid ovs_vport_free(struct vport *);
155ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
156ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#define VPORT_ALIGN 8
157ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
158ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/**
159ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *	vport_priv - access private data area of vport
160ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
161ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @vport: vport to access
162ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
163ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * If a nonzero size was passed in priv_size of vport_alloc() a private data
164ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * area was allocated on creation.  This allows that area to be accessed and
165ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * used for any purpose needed by the vport implementer.
166ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross */
167ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstatic inline void *vport_priv(const struct vport *vport)
168ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross{
169ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	return (u8 *)vport + ALIGN(sizeof(struct vport), VPORT_ALIGN);
170ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross}
171ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
172ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/**
173ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *	vport_from_priv - lookup vport from private data pointer
174ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
175ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * @priv: Start of private data area.
176ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross *
177ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * It is sometimes useful to translate from a pointer to the private data
178ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * area to the vport, such as in the case where the private data pointer is
179ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * the result of a hash table lookup.  @priv must point to the start of the
180ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * private data area.
181ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross */
182ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossstatic inline struct vport *vport_from_priv(const void *priv)
183ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross{
184ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross	return (struct vport *)(priv - ALIGN(sizeof(struct vport), VPORT_ALIGN));
185ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross}
186ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
187ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossvoid ovs_vport_receive(struct vport *, struct sk_buff *);
188ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossvoid ovs_vport_record_error(struct vport *, enum vport_err_type err_type);
189ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
190ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross/* List of statically compiled vport implementations.  Don't forget to also
191ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross * add yours to the list at the top of vport.c. */
192ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossextern const struct vport_ops ovs_netdev_vport_ops;
193ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Grossextern const struct vport_ops ovs_internal_vport_ops;
194ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross
195b34df5e805a6e98cae0bc5bc80c1b52d9ff811dePravin B Shelarstatic inline void ovs_skb_postpush_rcsum(struct sk_buff *skb,
196b34df5e805a6e98cae0bc5bc80c1b52d9ff811dePravin B Shelar				      const void *start, unsigned int len)
197b34df5e805a6e98cae0bc5bc80c1b52d9ff811dePravin B Shelar{
198b34df5e805a6e98cae0bc5bc80c1b52d9ff811dePravin B Shelar	if (skb->ip_summed == CHECKSUM_COMPLETE)
199b34df5e805a6e98cae0bc5bc80c1b52d9ff811dePravin B Shelar		skb->csum = csum_add(skb->csum, csum_partial(start, len, 0));
200b34df5e805a6e98cae0bc5bc80c1b52d9ff811dePravin B Shelar}
201b34df5e805a6e98cae0bc5bc80c1b52d9ff811dePravin B Shelar
202ccb1352e76cff0524e7ccb2074826a092dd13016Jesse Gross#endif /* vport.h */
203