xref: /openbmc/linux/net/openvswitch/datapath.h (revision cd8a6c33)
1ccb1352eSJesse Gross /*
2971427f3SAndy Zhou  * Copyright (c) 2007-2014 Nicira, Inc.
3ccb1352eSJesse Gross  *
4ccb1352eSJesse Gross  * This program is free software; you can redistribute it and/or
5ccb1352eSJesse Gross  * modify it under the terms of version 2 of the GNU General Public
6ccb1352eSJesse Gross  * License as published by the Free Software Foundation.
7ccb1352eSJesse Gross  *
8ccb1352eSJesse Gross  * This program is distributed in the hope that it will be useful, but
9ccb1352eSJesse Gross  * WITHOUT ANY WARRANTY; without even the implied warranty of
10ccb1352eSJesse Gross  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11ccb1352eSJesse Gross  * General Public License for more details.
12ccb1352eSJesse Gross  *
13ccb1352eSJesse Gross  * You should have received a copy of the GNU General Public License
14ccb1352eSJesse Gross  * along with this program; if not, write to the Free Software
15ccb1352eSJesse Gross  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16ccb1352eSJesse Gross  * 02110-1301, USA
17ccb1352eSJesse Gross  */
18ccb1352eSJesse Gross 
19ccb1352eSJesse Gross #ifndef DATAPATH_H
20ccb1352eSJesse Gross #define DATAPATH_H 1
21ccb1352eSJesse Gross 
22ccb1352eSJesse Gross #include <asm/page.h>
23ccb1352eSJesse Gross #include <linux/kernel.h>
24ccb1352eSJesse Gross #include <linux/mutex.h>
25ccb1352eSJesse Gross #include <linux/netdevice.h>
26ccb1352eSJesse Gross #include <linux/skbuff.h>
27ccb1352eSJesse Gross #include <linux/u64_stats_sync.h>
281d8fff90SThomas Graf #include <net/ip_tunnels.h>
29ccb1352eSJesse Gross 
307f8a436eSJoe Stringer #include "conntrack.h"
31ccb1352eSJesse Gross #include "flow.h"
32e6445719SPravin B Shelar #include "flow_table.h"
33cd8a6c33SAndy Zhou #include "meter.h"
349602c01eSAndy Zhou #include "vport-internal_dev.h"
35ccb1352eSJesse Gross 
3615eac2a7SPravin B Shelar #define DP_MAX_PORTS           USHRT_MAX
3715eac2a7SPravin B Shelar #define DP_VPORT_HASH_BUCKETS  1024
3815eac2a7SPravin B Shelar 
39ccb1352eSJesse Gross /**
40ccb1352eSJesse Gross  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
41ccb1352eSJesse Gross  * datapath.
42ccb1352eSJesse Gross  * @n_hit: Number of received packets for which a matching flow was found in
43ccb1352eSJesse Gross  * the flow table.
44ccb1352eSJesse Gross  * @n_miss: Number of received packets that had no matching flow in the flow
45ccb1352eSJesse Gross  * table.  The sum of @n_hit and @n_miss is the number of packets that have
46ccb1352eSJesse Gross  * been received by the datapath.
47ccb1352eSJesse Gross  * @n_lost: Number of received packets that had no matching flow in the flow
48ccb1352eSJesse Gross  * table that could not be sent to userspace (normally due to an overflow in
49ccb1352eSJesse Gross  * one of the datapath's queues).
501bd7116fSAndy Zhou  * @n_mask_hit: Number of masks looked up for flow match.
511bd7116fSAndy Zhou  *   @n_mask_hit / (@n_hit + @n_missed)  will be the average masks looked
521bd7116fSAndy Zhou  *   up per packet.
53ccb1352eSJesse Gross  */
54ccb1352eSJesse Gross struct dp_stats_percpu {
55ccb1352eSJesse Gross 	u64 n_hit;
56ccb1352eSJesse Gross 	u64 n_missed;
57ccb1352eSJesse Gross 	u64 n_lost;
581bd7116fSAndy Zhou 	u64 n_mask_hit;
59df9d9fdfSWANG Cong 	struct u64_stats_sync syncp;
60ccb1352eSJesse Gross };
61ccb1352eSJesse Gross 
62ccb1352eSJesse Gross /**
63ccb1352eSJesse Gross  * struct datapath - datapath for flow-based packet switching
64ccb1352eSJesse Gross  * @rcu: RCU callback head for deferred destruction.
65ccb1352eSJesse Gross  * @list_node: Element in global 'dps' list.
66b637e498SPravin B Shelar  * @table: flow table.
6715eac2a7SPravin B Shelar  * @ports: Hash table for ports.  %OVSP_LOCAL port always exists.  Protected by
688e4e1713SPravin B Shelar  * ovs_mutex and RCU.
69ccb1352eSJesse Gross  * @stats_percpu: Per-CPU datapath statistics.
7046df7b81SPravin B Shelar  * @net: Reference to net namespace.
713a927bc7SPaolo Abeni  * @max_headroom: the maximum headroom of all vports in this datapath; it will
723a927bc7SPaolo Abeni  * be used by all the internal vports in this dp.
73ccb1352eSJesse Gross  *
74ccb1352eSJesse Gross  * Context: See the comment on locking at the top of datapath.c for additional
75ccb1352eSJesse Gross  * locking information.
76ccb1352eSJesse Gross  */
77ccb1352eSJesse Gross struct datapath {
78ccb1352eSJesse Gross 	struct rcu_head rcu;
79ccb1352eSJesse Gross 	struct list_head list_node;
80ccb1352eSJesse Gross 
81ccb1352eSJesse Gross 	/* Flow table. */
82b637e498SPravin B Shelar 	struct flow_table table;
83ccb1352eSJesse Gross 
84ccb1352eSJesse Gross 	/* Switch ports. */
8515eac2a7SPravin B Shelar 	struct hlist_head *ports;
86ccb1352eSJesse Gross 
87ccb1352eSJesse Gross 	/* Stats. */
88ccb1352eSJesse Gross 	struct dp_stats_percpu __percpu *stats_percpu;
8946df7b81SPravin B Shelar 
9046df7b81SPravin B Shelar 	/* Network namespace ref. */
910c5c9fb5SEric W. Biederman 	possible_net_t net;
9243d4be9cSThomas Graf 
9343d4be9cSThomas Graf 	u32 user_features;
943a927bc7SPaolo Abeni 
953a927bc7SPaolo Abeni 	u32 max_headroom;
9696fbc13dSAndy Zhou 
9796fbc13dSAndy Zhou 	/* Switch meters. */
9896fbc13dSAndy Zhou 	struct hlist_head *meters;
99ccb1352eSJesse Gross };
100ccb1352eSJesse Gross 
101ccb1352eSJesse Gross /**
102ccb1352eSJesse Gross  * struct ovs_skb_cb - OVS data in skb CB
10383c8df26SPravin B Shelar  * @input_vport: The original vport packet came in on. This value is cached
10483c8df26SPravin B Shelar  * when a packet is received by OVS.
1057f8a436eSJoe Stringer  * @mru: The maximum received fragement size; 0 if the packet is not
1067f8a436eSJoe Stringer  * fragmented.
107494bea39SLiping Zhang  * @acts_origlen: The netlink size of the flow actions applied to this skb.
10852427fa0SDaniel Axtens  * @cutlen: The number of bytes from the packet end to be removed.
109ccb1352eSJesse Gross  */
110ccb1352eSJesse Gross struct ovs_skb_cb {
11183c8df26SPravin B Shelar 	struct vport		*input_vport;
1127f8a436eSJoe Stringer 	u16			mru;
113494bea39SLiping Zhang 	u16			acts_origlen;
114f2a4d086SWilliam Tu 	u32			cutlen;
115ccb1352eSJesse Gross };
116ccb1352eSJesse Gross #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
117ccb1352eSJesse Gross 
118ccb1352eSJesse Gross /**
119ccb1352eSJesse Gross  * struct dp_upcall - metadata to include with a packet to send to userspace
120ccb1352eSJesse Gross  * @cmd: One of %OVS_PACKET_CMD_*.
1214490108bSBen Pfaff  * @userdata: If nonnull, its variable-length value is passed to userspace as
122ccb1352eSJesse Gross  * %OVS_PACKET_ATTR_USERDATA.
123e8eedb85SPravin B Shelar  * @portid: Netlink portid to which packet should be sent.  If @portid is 0
124e8eedb85SPravin B Shelar  * then no packet is sent and the packet is accounted in the datapath's @n_lost
125ccb1352eSJesse Gross  * counter.
1268f0aad6fSWenyu Zhang  * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
1277f8a436eSJoe Stringer  * @mru: If not zero, Maximum received IP fragment size.
128ccb1352eSJesse Gross  */
129ccb1352eSJesse Gross struct dp_upcall_info {
1304c222798SPravin B Shelar 	struct ip_tunnel_info *egress_tun_info;
131ccb1352eSJesse Gross 	const struct nlattr *userdata;
132ccea7445SNeil McKee 	const struct nlattr *actions;
133ccea7445SNeil McKee 	int actions_len;
13415e47304SEric W. Biederman 	u32 portid;
135e8eedb85SPravin B Shelar 	u8 cmd;
1367f8a436eSJoe Stringer 	u16 mru;
137ccb1352eSJesse Gross };
138ccb1352eSJesse Gross 
1398e4e1713SPravin B Shelar /**
1408e4e1713SPravin B Shelar  * struct ovs_net - Per net-namespace data for ovs.
1418e4e1713SPravin B Shelar  * @dps: List of datapaths to enable dumping them all out.
1428e4e1713SPravin B Shelar  * Protected by genl_mutex.
1438e4e1713SPravin B Shelar  */
1448e4e1713SPravin B Shelar struct ovs_net {
1458e4e1713SPravin B Shelar 	struct list_head dps;
1468e4e1713SPravin B Shelar 	struct work_struct dp_notify_work;
147c2ac6673SJoe Stringer 
148c2ac6673SJoe Stringer 	/* Module reference for configuring conntrack. */
149c2ac6673SJoe Stringer 	bool xt_label;
1508e4e1713SPravin B Shelar };
1518e4e1713SPravin B Shelar 
152c7d03a00SAlexey Dobriyan extern unsigned int ovs_net_id;
1538e4e1713SPravin B Shelar void ovs_lock(void);
1548e4e1713SPravin B Shelar void ovs_unlock(void);
1558e4e1713SPravin B Shelar 
1568e4e1713SPravin B Shelar #ifdef CONFIG_LOCKDEP
1578e4e1713SPravin B Shelar int lockdep_ovsl_is_held(void);
1588e4e1713SPravin B Shelar #else
1598e4e1713SPravin B Shelar #define lockdep_ovsl_is_held()	1
1608e4e1713SPravin B Shelar #endif
1618e4e1713SPravin B Shelar 
16280019d31SThomas Graf #define ASSERT_OVSL()		WARN_ON(!lockdep_ovsl_is_held())
1638e4e1713SPravin B Shelar #define ovsl_dereference(p)					\
1648e4e1713SPravin B Shelar 	rcu_dereference_protected(p, lockdep_ovsl_is_held())
165663efa36SJesse Gross #define rcu_dereference_ovsl(p)					\
166663efa36SJesse Gross 	rcu_dereference_check(p, lockdep_ovsl_is_held())
1678e4e1713SPravin B Shelar 
16812eb18f7SThomas Graf static inline struct net *ovs_dp_get_net(const struct datapath *dp)
16946df7b81SPravin B Shelar {
17046df7b81SPravin B Shelar 	return read_pnet(&dp->net);
17146df7b81SPravin B Shelar }
17246df7b81SPravin B Shelar 
17346df7b81SPravin B Shelar static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
17446df7b81SPravin B Shelar {
17546df7b81SPravin B Shelar 	write_pnet(&dp->net, net);
17646df7b81SPravin B Shelar }
17746df7b81SPravin B Shelar 
1788e4e1713SPravin B Shelar struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
1798e4e1713SPravin B Shelar 
1808e4e1713SPravin B Shelar static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
1818e4e1713SPravin B Shelar {
1828e4e1713SPravin B Shelar 	WARN_ON_ONCE(!rcu_read_lock_held());
1838e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1848e4e1713SPravin B Shelar }
1858e4e1713SPravin B Shelar 
1868e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
1878e4e1713SPravin B Shelar {
1888e4e1713SPravin B Shelar 	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
1898e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1908e4e1713SPravin B Shelar }
1918e4e1713SPravin B Shelar 
1928e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
1938e4e1713SPravin B Shelar {
1948e4e1713SPravin B Shelar 	ASSERT_OVSL();
1958e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1968e4e1713SPravin B Shelar }
1978e4e1713SPravin B Shelar 
1989602c01eSAndy Zhou /* Must be called with rcu_read_lock. */
1999602c01eSAndy Zhou static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
2009602c01eSAndy Zhou {
2019602c01eSAndy Zhou 	struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
2029602c01eSAndy Zhou 
2039602c01eSAndy Zhou 	if (dev) {
2049602c01eSAndy Zhou 		struct vport *vport = ovs_internal_dev_get_vport(dev);
2059602c01eSAndy Zhou 
2069602c01eSAndy Zhou 		if (vport)
2079602c01eSAndy Zhou 			return vport->dp;
2089602c01eSAndy Zhou 	}
2099602c01eSAndy Zhou 
2109602c01eSAndy Zhou 	return NULL;
2119602c01eSAndy Zhou }
2129602c01eSAndy Zhou 
2139602c01eSAndy Zhou /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
2149602c01eSAndy Zhou  * returned dp pointer valid.
2159602c01eSAndy Zhou  */
2169602c01eSAndy Zhou static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
2179602c01eSAndy Zhou {
2189602c01eSAndy Zhou 	struct datapath *dp;
2199602c01eSAndy Zhou 
2209602c01eSAndy Zhou 	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
2219602c01eSAndy Zhou 	rcu_read_lock();
2229602c01eSAndy Zhou 	dp = get_dp_rcu(net, dp_ifindex);
2239602c01eSAndy Zhou 	rcu_read_unlock();
2249602c01eSAndy Zhou 
2259602c01eSAndy Zhou 	return dp;
2269602c01eSAndy Zhou }
2279602c01eSAndy Zhou 
228ccb1352eSJesse Gross extern struct notifier_block ovs_dp_device_notifier;
22968eb5503SJohannes Berg extern struct genl_family dp_vport_genl_family;
230ccb1352eSJesse Gross 
2318c8b1b83SPravin B Shelar void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
232ccb1352eSJesse Gross void ovs_dp_detach_port(struct vport *);
233ccb1352eSJesse Gross int ovs_dp_upcall(struct datapath *, struct sk_buff *,
234f2a4d086SWilliam Tu 		  const struct sw_flow_key *, const struct dp_upcall_info *,
235f2a4d086SWilliam Tu 		  uint32_t cutlen);
236ccb1352eSJesse Gross 
237971427f3SAndy Zhou const char *ovs_dp_name(const struct datapath *dp);
2389354d452SJiri Benc struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
2399354d452SJiri Benc 					 u32 portid, u32 seq, u8 cmd);
240ccb1352eSJesse Gross 
2412ff3e4e4SPravin B Shelar int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
24212eb18f7SThomas Graf 			const struct sw_flow_actions *, struct sw_flow_key *);
243971427f3SAndy Zhou 
2448e4e1713SPravin B Shelar void ovs_dp_notify_wq(struct work_struct *work);
24503f0d916SAndy Zhou 
246971427f3SAndy Zhou int action_fifos_init(void);
247971427f3SAndy Zhou void action_fifos_exit(void);
248971427f3SAndy Zhou 
249be26b9a8SJoe Stringer /* 'KEY' must not have any bits set outside of the 'MASK' */
250be26b9a8SJoe Stringer #define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
251be26b9a8SJoe Stringer #define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
252be26b9a8SJoe Stringer 
25305da5898SJarno Rajahalme #define OVS_NLERR(logging_allowed, fmt, ...)			\
2541815a883SJoe Perches do {								\
25505da5898SJarno Rajahalme 	if (logging_allowed && net_ratelimit())			\
25605da5898SJarno Rajahalme 		pr_info("netlink: " fmt "\n", ##__VA_ARGS__);	\
2571815a883SJoe Perches } while (0)
258ccb1352eSJesse Gross #endif /* datapath.h */
259