/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright (c) 2007-2012 Nicira, Inc.3*/45#ifndef VPORT_H6#define VPORT_H 178#include <linux/if_tunnel.h>9#include <linux/list.h>10#include <linux/netlink.h>11#include <linux/openvswitch.h>12#include <linux/reciprocal_div.h>13#include <linux/skbuff.h>14#include <linux/spinlock.h>15#include <linux/u64_stats_sync.h>1617#include "datapath.h"1819struct vport;20struct vport_parms;2122/* The following definitions are for users of the vport subsystem: */2324int ovs_vport_init(void);25void ovs_vport_exit(void);2627struct vport *ovs_vport_add(const struct vport_parms *);28void ovs_vport_del(struct vport *);2930struct vport *ovs_vport_locate(const struct net *net, const char *name);3132void ovs_vport_get_stats(struct vport *, struct ovs_vport_stats *);3334int ovs_vport_get_upcall_stats(struct vport *vport, struct sk_buff *skb);3536int ovs_vport_set_options(struct vport *, struct nlattr *options);37int ovs_vport_get_options(const struct vport *, struct sk_buff *);3839int ovs_vport_set_upcall_portids(struct vport *, const struct nlattr *pids);40int ovs_vport_get_upcall_portids(const struct vport *, struct sk_buff *);41u32 ovs_vport_find_upcall_portid(const struct vport *, struct sk_buff *);4243/**44* struct vport_portids - array of netlink portids of a vport.45* must be protected by rcu.46* @rn_ids: The reciprocal value of @n_ids.47* @rcu: RCU callback head for deferred destruction.48* @n_ids: Size of @ids array.49* @ids: Array storing the Netlink socket pids to be used for packets received50* on this port that miss the flow table.51*/52struct vport_portids {53struct reciprocal_value rn_ids;54struct rcu_head rcu;55u32 n_ids;56u32 ids[];57};5859/**60* struct vport - one port within a datapath61* @dev: Pointer to net_device.62* @dev_tracker: refcount tracker for @dev reference63* @dp: Datapath to which this port belongs.64* @upcall_portids: RCU protected 'struct vport_portids'.65* @port_no: Index into @dp's @ports array.66* @hash_node: Element in @dev_table hash table in vport.c.67* @dp_hash_node: Element in @datapath->ports hash table in datapath.c.68* @ops: Class structure.69* @upcall_stats: Upcall stats of every ports.70* @detach_list: list used for detaching vport in net-exit call.71* @rcu: RCU callback head for deferred destruction.72*/73struct vport {74struct net_device *dev;75netdevice_tracker dev_tracker;76struct datapath *dp;77struct vport_portids __rcu *upcall_portids;78u16 port_no;7980struct hlist_node hash_node;81struct hlist_node dp_hash_node;82const struct vport_ops *ops;83struct vport_upcall_stats_percpu __percpu *upcall_stats;8485struct list_head detach_list;86struct rcu_head rcu;87};8889/**90* struct vport_parms - parameters for creating a new vport91*92* @name: New vport's name.93* @type: New vport's type.94* @options: %OVS_VPORT_ATTR_OPTIONS attribute from Netlink message, %NULL if95* none was supplied.96* @desired_ifindex: New vport's ifindex.97* @dp: New vport's datapath.98* @port_no: New vport's port number.99* @upcall_portids: %OVS_VPORT_ATTR_UPCALL_PID attribute from Netlink message,100* %NULL if none was supplied.101*/102struct vport_parms {103const char *name;104enum ovs_vport_type type;105int desired_ifindex;106struct nlattr *options;107108/* For ovs_vport_alloc(). */109struct datapath *dp;110u16 port_no;111struct nlattr *upcall_portids;112};113114/**115* struct vport_ops - definition of a type of virtual port116*117* @type: %OVS_VPORT_TYPE_* value for this type of virtual port.118* @create: Create a new vport configured as specified. On success returns119* a new vport allocated with ovs_vport_alloc(), otherwise an ERR_PTR() value.120* @destroy: Destroys a vport. Must call vport_free() on the vport but not121* before an RCU grace period has elapsed.122* @set_options: Modify the configuration of an existing vport. May be %NULL123* if modification is not supported.124* @get_options: Appends vport-specific attributes for the configuration of an125* existing vport to a &struct sk_buff. May be %NULL for a vport that does not126* have any configuration.127* @send: Send a packet on the device.128* zero for dropped packets or negative for error.129* @owner: Module that implements this vport type.130* @list: List entry in the global list of vport types.131*/132struct vport_ops {133enum ovs_vport_type type;134135/* Called with ovs_mutex. */136struct vport *(*create)(const struct vport_parms *);137void (*destroy)(struct vport *);138139int (*set_options)(struct vport *, struct nlattr *);140int (*get_options)(const struct vport *, struct sk_buff *);141142int (*send)(struct sk_buff *skb);143struct module *owner;144struct list_head list;145};146147/**148* struct vport_upcall_stats_percpu - per-cpu packet upcall statistics for149* a given vport.150* @syncp: Synchronization point for 64bit counters.151* @n_success: Number of packets that upcall to userspace succeed.152* @n_fail: Number of packets that upcall to userspace failed.153*/154struct vport_upcall_stats_percpu {155struct u64_stats_sync syncp;156u64_stats_t n_success;157u64_stats_t n_fail;158};159160struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *,161const struct vport_parms *);162void ovs_vport_free(struct vport *);163164#define VPORT_ALIGN 8165166/**167* vport_priv - access private data area of vport168*169* @vport: vport to access170*171* Returns: A void pointer to a private data allocated in the @vport.172*173* If a nonzero size was passed in priv_size of vport_alloc() a private data174* area was allocated on creation. This allows that area to be accessed and175* used for any purpose needed by the vport implementer.176*/177static inline void *vport_priv(const struct vport *vport)178{179return (u8 *)(uintptr_t)vport + ALIGN(sizeof(struct vport), VPORT_ALIGN);180}181182/**183* vport_from_priv - lookup vport from private data pointer184*185* @priv: Start of private data area.186*187* Returns: A reference to a vport structure that contains @priv.188*189* It is sometimes useful to translate from a pointer to the private data190* area to the vport, such as in the case where the private data pointer is191* the result of a hash table lookup. @priv must point to the start of the192* private data area.193*/194static inline struct vport *vport_from_priv(void *priv)195{196return (struct vport *)((u8 *)priv - ALIGN(sizeof(struct vport), VPORT_ALIGN));197}198199int ovs_vport_receive(struct vport *, struct sk_buff *,200const struct ip_tunnel_info *);201202static inline const char *ovs_vport_name(struct vport *vport)203{204return vport->dev->name;205}206207int __ovs_vport_ops_register(struct vport_ops *ops);208#define ovs_vport_ops_register(ops) \209({ \210(ops)->owner = THIS_MODULE; \211__ovs_vport_ops_register(ops); \212})213214void ovs_vport_ops_unregister(struct vport_ops *ops);215void ovs_vport_send(struct vport *vport, struct sk_buff *skb, u8 mac_proto);216217#endif /* vport.h */218219220