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