1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2007-2012 Nicira, Inc.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #ifndef VPORT_H
7*4882a593Smuzhiyun #define VPORT_H 1
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <linux/if_tunnel.h>
10*4882a593Smuzhiyun #include <linux/list.h>
11*4882a593Smuzhiyun #include <linux/netlink.h>
12*4882a593Smuzhiyun #include <linux/openvswitch.h>
13*4882a593Smuzhiyun #include <linux/reciprocal_div.h>
14*4882a593Smuzhiyun #include <linux/skbuff.h>
15*4882a593Smuzhiyun #include <linux/spinlock.h>
16*4882a593Smuzhiyun #include <linux/u64_stats_sync.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "datapath.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun struct vport;
21*4882a593Smuzhiyun struct vport_parms;
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* The following definitions are for users of the vport subsytem: */
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun int ovs_vport_init(void);
26*4882a593Smuzhiyun void ovs_vport_exit(void);
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun struct vport *ovs_vport_add(const struct vport_parms *);
29*4882a593Smuzhiyun void ovs_vport_del(struct vport *);
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct vport *ovs_vport_locate(const struct net *net, const char *name);
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun void ovs_vport_get_stats(struct vport *, struct ovs_vport_stats *);
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun int ovs_vport_set_options(struct vport *, struct nlattr *options);
36*4882a593Smuzhiyun int ovs_vport_get_options(const struct vport *, struct sk_buff *);
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun int ovs_vport_set_upcall_portids(struct vport *, const struct nlattr *pids);
39*4882a593Smuzhiyun int ovs_vport_get_upcall_portids(const struct vport *, struct sk_buff *);
40*4882a593Smuzhiyun u32 ovs_vport_find_upcall_portid(const struct vport *, struct sk_buff *);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /**
43*4882a593Smuzhiyun * struct vport_portids - array of netlink portids of a vport.
44*4882a593Smuzhiyun * must be protected by rcu.
45*4882a593Smuzhiyun * @rn_ids: The reciprocal value of @n_ids.
46*4882a593Smuzhiyun * @rcu: RCU callback head for deferred destruction.
47*4882a593Smuzhiyun * @n_ids: Size of @ids array.
48*4882a593Smuzhiyun * @ids: Array storing the Netlink socket pids to be used for packets received
49*4882a593Smuzhiyun * on this port that miss the flow table.
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun struct vport_portids {
52*4882a593Smuzhiyun struct reciprocal_value rn_ids;
53*4882a593Smuzhiyun struct rcu_head rcu;
54*4882a593Smuzhiyun u32 n_ids;
55*4882a593Smuzhiyun u32 ids[];
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /**
59*4882a593Smuzhiyun * struct vport - one port within a datapath
60*4882a593Smuzhiyun * @dev: Pointer to net_device.
61*4882a593Smuzhiyun * @dp: Datapath to which this port belongs.
62*4882a593Smuzhiyun * @upcall_portids: RCU protected 'struct vport_portids'.
63*4882a593Smuzhiyun * @port_no: Index into @dp's @ports array.
64*4882a593Smuzhiyun * @hash_node: Element in @dev_table hash table in vport.c.
65*4882a593Smuzhiyun * @dp_hash_node: Element in @datapath->ports hash table in datapath.c.
66*4882a593Smuzhiyun * @ops: Class structure.
67*4882a593Smuzhiyun * @detach_list: list used for detaching vport in net-exit call.
68*4882a593Smuzhiyun * @rcu: RCU callback head for deferred destruction.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun struct vport {
71*4882a593Smuzhiyun struct net_device *dev;
72*4882a593Smuzhiyun struct datapath *dp;
73*4882a593Smuzhiyun struct vport_portids __rcu *upcall_portids;
74*4882a593Smuzhiyun u16 port_no;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun struct hlist_node hash_node;
77*4882a593Smuzhiyun struct hlist_node dp_hash_node;
78*4882a593Smuzhiyun const struct vport_ops *ops;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun struct list_head detach_list;
81*4882a593Smuzhiyun struct rcu_head rcu;
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /**
85*4882a593Smuzhiyun * struct vport_parms - parameters for creating a new vport
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * @name: New vport's name.
88*4882a593Smuzhiyun * @type: New vport's type.
89*4882a593Smuzhiyun * @options: %OVS_VPORT_ATTR_OPTIONS attribute from Netlink message, %NULL if
90*4882a593Smuzhiyun * none was supplied.
91*4882a593Smuzhiyun * @dp: New vport's datapath.
92*4882a593Smuzhiyun * @port_no: New vport's port number.
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun struct vport_parms {
95*4882a593Smuzhiyun const char *name;
96*4882a593Smuzhiyun enum ovs_vport_type type;
97*4882a593Smuzhiyun struct nlattr *options;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* For ovs_vport_alloc(). */
100*4882a593Smuzhiyun struct datapath *dp;
101*4882a593Smuzhiyun u16 port_no;
102*4882a593Smuzhiyun struct nlattr *upcall_portids;
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /**
106*4882a593Smuzhiyun * struct vport_ops - definition of a type of virtual port
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * @type: %OVS_VPORT_TYPE_* value for this type of virtual port.
109*4882a593Smuzhiyun * @create: Create a new vport configured as specified. On success returns
110*4882a593Smuzhiyun * a new vport allocated with ovs_vport_alloc(), otherwise an ERR_PTR() value.
111*4882a593Smuzhiyun * @destroy: Destroys a vport. Must call vport_free() on the vport but not
112*4882a593Smuzhiyun * before an RCU grace period has elapsed.
113*4882a593Smuzhiyun * @set_options: Modify the configuration of an existing vport. May be %NULL
114*4882a593Smuzhiyun * if modification is not supported.
115*4882a593Smuzhiyun * @get_options: Appends vport-specific attributes for the configuration of an
116*4882a593Smuzhiyun * existing vport to a &struct sk_buff. May be %NULL for a vport that does not
117*4882a593Smuzhiyun * have any configuration.
118*4882a593Smuzhiyun * @send: Send a packet on the device.
119*4882a593Smuzhiyun * zero for dropped packets or negative for error.
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun struct vport_ops {
122*4882a593Smuzhiyun enum ovs_vport_type type;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /* Called with ovs_mutex. */
125*4882a593Smuzhiyun struct vport *(*create)(const struct vport_parms *);
126*4882a593Smuzhiyun void (*destroy)(struct vport *);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun int (*set_options)(struct vport *, struct nlattr *);
129*4882a593Smuzhiyun int (*get_options)(const struct vport *, struct sk_buff *);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun netdev_tx_t (*send) (struct sk_buff *skb);
132*4882a593Smuzhiyun struct module *owner;
133*4882a593Smuzhiyun struct list_head list;
134*4882a593Smuzhiyun };
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *,
137*4882a593Smuzhiyun const struct vport_parms *);
138*4882a593Smuzhiyun void ovs_vport_free(struct vport *);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun #define VPORT_ALIGN 8
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /**
143*4882a593Smuzhiyun * vport_priv - access private data area of vport
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * @vport: vport to access
146*4882a593Smuzhiyun *
147*4882a593Smuzhiyun * If a nonzero size was passed in priv_size of vport_alloc() a private data
148*4882a593Smuzhiyun * area was allocated on creation. This allows that area to be accessed and
149*4882a593Smuzhiyun * used for any purpose needed by the vport implementer.
150*4882a593Smuzhiyun */
vport_priv(const struct vport * vport)151*4882a593Smuzhiyun static inline void *vport_priv(const struct vport *vport)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun return (u8 *)(uintptr_t)vport + ALIGN(sizeof(struct vport), VPORT_ALIGN);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /**
157*4882a593Smuzhiyun * vport_from_priv - lookup vport from private data pointer
158*4882a593Smuzhiyun *
159*4882a593Smuzhiyun * @priv: Start of private data area.
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * It is sometimes useful to translate from a pointer to the private data
162*4882a593Smuzhiyun * area to the vport, such as in the case where the private data pointer is
163*4882a593Smuzhiyun * the result of a hash table lookup. @priv must point to the start of the
164*4882a593Smuzhiyun * private data area.
165*4882a593Smuzhiyun */
vport_from_priv(void * priv)166*4882a593Smuzhiyun static inline struct vport *vport_from_priv(void *priv)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun return (struct vport *)((u8 *)priv - ALIGN(sizeof(struct vport), VPORT_ALIGN));
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun int ovs_vport_receive(struct vport *, struct sk_buff *,
172*4882a593Smuzhiyun const struct ip_tunnel_info *);
173*4882a593Smuzhiyun
ovs_vport_name(struct vport * vport)174*4882a593Smuzhiyun static inline const char *ovs_vport_name(struct vport *vport)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun return vport->dev->name;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun int __ovs_vport_ops_register(struct vport_ops *ops);
180*4882a593Smuzhiyun #define ovs_vport_ops_register(ops) \
181*4882a593Smuzhiyun ({ \
182*4882a593Smuzhiyun (ops)->owner = THIS_MODULE; \
183*4882a593Smuzhiyun __ovs_vport_ops_register(ops); \
184*4882a593Smuzhiyun })
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun void ovs_vport_ops_unregister(struct vport_ops *ops);
187*4882a593Smuzhiyun void ovs_vport_send(struct vport *vport, struct sk_buff *skb, u8 mac_proto);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun #endif /* vport.h */
190