xref: /openbmc/linux/net/openvswitch/datapath.h (revision 80019d31)
1ccb1352eSJesse Gross /*
2caf2ee14SRaju Subramanian  * Copyright (c) 2007-2012 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>
28ccb1352eSJesse Gross 
29ccb1352eSJesse Gross #include "flow.h"
30e6445719SPravin B Shelar #include "flow_table.h"
3146df7b81SPravin B Shelar #include "vport.h"
32ccb1352eSJesse Gross 
3315eac2a7SPravin B Shelar #define DP_MAX_PORTS           USHRT_MAX
3415eac2a7SPravin B Shelar #define DP_VPORT_HASH_BUCKETS  1024
3515eac2a7SPravin B Shelar 
36ccb1352eSJesse Gross #define SAMPLE_ACTION_DEPTH 3
37ccb1352eSJesse Gross 
38ccb1352eSJesse Gross /**
39ccb1352eSJesse Gross  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
40ccb1352eSJesse Gross  * datapath.
41ccb1352eSJesse Gross  * @n_hit: Number of received packets for which a matching flow was found in
42ccb1352eSJesse Gross  * the flow table.
43ccb1352eSJesse Gross  * @n_miss: Number of received packets that had no matching flow in the flow
44ccb1352eSJesse Gross  * table.  The sum of @n_hit and @n_miss is the number of packets that have
45ccb1352eSJesse Gross  * been received by the datapath.
46ccb1352eSJesse Gross  * @n_lost: Number of received packets that had no matching flow in the flow
47ccb1352eSJesse Gross  * table that could not be sent to userspace (normally due to an overflow in
48ccb1352eSJesse Gross  * one of the datapath's queues).
491bd7116fSAndy Zhou  * @n_mask_hit: Number of masks looked up for flow match.
501bd7116fSAndy Zhou  *   @n_mask_hit / (@n_hit + @n_missed)  will be the average masks looked
511bd7116fSAndy Zhou  *   up per packet.
52ccb1352eSJesse Gross  */
53ccb1352eSJesse Gross struct dp_stats_percpu {
54ccb1352eSJesse Gross 	u64 n_hit;
55ccb1352eSJesse Gross 	u64 n_missed;
56ccb1352eSJesse Gross 	u64 n_lost;
571bd7116fSAndy Zhou 	u64 n_mask_hit;
58df9d9fdfSWANG Cong 	struct u64_stats_sync syncp;
59ccb1352eSJesse Gross };
60ccb1352eSJesse Gross 
61ccb1352eSJesse Gross /**
62ccb1352eSJesse Gross  * struct datapath - datapath for flow-based packet switching
63ccb1352eSJesse Gross  * @rcu: RCU callback head for deferred destruction.
64ccb1352eSJesse Gross  * @list_node: Element in global 'dps' list.
65b637e498SPravin B Shelar  * @table: flow table.
6615eac2a7SPravin B Shelar  * @ports: Hash table for ports.  %OVSP_LOCAL port always exists.  Protected by
678e4e1713SPravin B Shelar  * ovs_mutex and RCU.
68ccb1352eSJesse Gross  * @stats_percpu: Per-CPU datapath statistics.
6946df7b81SPravin B Shelar  * @net: Reference to net namespace.
70ccb1352eSJesse Gross  *
71ccb1352eSJesse Gross  * Context: See the comment on locking at the top of datapath.c for additional
72ccb1352eSJesse Gross  * locking information.
73ccb1352eSJesse Gross  */
74ccb1352eSJesse Gross struct datapath {
75ccb1352eSJesse Gross 	struct rcu_head rcu;
76ccb1352eSJesse Gross 	struct list_head list_node;
77ccb1352eSJesse Gross 
78ccb1352eSJesse Gross 	/* Flow table. */
79b637e498SPravin B Shelar 	struct flow_table table;
80ccb1352eSJesse Gross 
81ccb1352eSJesse Gross 	/* Switch ports. */
8215eac2a7SPravin B Shelar 	struct hlist_head *ports;
83ccb1352eSJesse Gross 
84ccb1352eSJesse Gross 	/* Stats. */
85ccb1352eSJesse Gross 	struct dp_stats_percpu __percpu *stats_percpu;
8646df7b81SPravin B Shelar 
8746df7b81SPravin B Shelar #ifdef CONFIG_NET_NS
8846df7b81SPravin B Shelar 	/* Network namespace ref. */
8946df7b81SPravin B Shelar 	struct net *net;
9046df7b81SPravin B Shelar #endif
9143d4be9cSThomas Graf 
9243d4be9cSThomas Graf 	u32 user_features;
93ccb1352eSJesse Gross };
94ccb1352eSJesse Gross 
95ccb1352eSJesse Gross /**
96ccb1352eSJesse Gross  * struct ovs_skb_cb - OVS data in skb CB
97ccb1352eSJesse Gross  * @flow: The flow associated with this packet.  May be %NULL if no flow.
9803f0d916SAndy Zhou  * @pkt_key: The flow information extracted from the packet.  Must be nonnull.
997d5437c7SPravin B Shelar  * @tun_key: Key for the tunnel that encapsulated this packet. NULL if the
1007d5437c7SPravin B Shelar  * packet is not being tunneled.
101ccb1352eSJesse Gross  */
102ccb1352eSJesse Gross struct ovs_skb_cb {
103ccb1352eSJesse Gross 	struct sw_flow		*flow;
10403f0d916SAndy Zhou 	struct sw_flow_key	*pkt_key;
1057d5437c7SPravin B Shelar 	struct ovs_key_ipv4_tunnel  *tun_key;
106ccb1352eSJesse Gross };
107ccb1352eSJesse Gross #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
108ccb1352eSJesse Gross 
109ccb1352eSJesse Gross /**
110ccb1352eSJesse Gross  * struct dp_upcall - metadata to include with a packet to send to userspace
111ccb1352eSJesse Gross  * @cmd: One of %OVS_PACKET_CMD_*.
112ccb1352eSJesse Gross  * @key: Becomes %OVS_PACKET_ATTR_KEY.  Must be nonnull.
1134490108bSBen Pfaff  * @userdata: If nonnull, its variable-length value is passed to userspace as
114ccb1352eSJesse Gross  * %OVS_PACKET_ATTR_USERDATA.
115ccb1352eSJesse Gross  * @pid: Netlink PID to which packet should be sent.  If @pid is 0 then no
116ccb1352eSJesse Gross  * packet is sent and the packet is accounted in the datapath's @n_lost
117ccb1352eSJesse Gross  * counter.
118ccb1352eSJesse Gross  */
119ccb1352eSJesse Gross struct dp_upcall_info {
120ccb1352eSJesse Gross 	u8 cmd;
121ccb1352eSJesse Gross 	const struct sw_flow_key *key;
122ccb1352eSJesse Gross 	const struct nlattr *userdata;
12315e47304SEric W. Biederman 	u32 portid;
124ccb1352eSJesse Gross };
125ccb1352eSJesse Gross 
1268e4e1713SPravin B Shelar /**
1278e4e1713SPravin B Shelar  * struct ovs_net - Per net-namespace data for ovs.
1288e4e1713SPravin B Shelar  * @dps: List of datapaths to enable dumping them all out.
1298e4e1713SPravin B Shelar  * Protected by genl_mutex.
1308e4e1713SPravin B Shelar  */
1318e4e1713SPravin B Shelar struct ovs_net {
1328e4e1713SPravin B Shelar 	struct list_head dps;
1338e4e1713SPravin B Shelar 	struct work_struct dp_notify_work;
134aa310701SPravin B Shelar 	struct vport_net vport_net;
1358e4e1713SPravin B Shelar };
1368e4e1713SPravin B Shelar 
1378e4e1713SPravin B Shelar extern int ovs_net_id;
1388e4e1713SPravin B Shelar void ovs_lock(void);
1398e4e1713SPravin B Shelar void ovs_unlock(void);
1408e4e1713SPravin B Shelar 
1418e4e1713SPravin B Shelar #ifdef CONFIG_LOCKDEP
1428e4e1713SPravin B Shelar int lockdep_ovsl_is_held(void);
1438e4e1713SPravin B Shelar #else
1448e4e1713SPravin B Shelar #define lockdep_ovsl_is_held()	1
1458e4e1713SPravin B Shelar #endif
1468e4e1713SPravin B Shelar 
14780019d31SThomas Graf #define ASSERT_OVSL()		WARN_ON(!lockdep_ovsl_is_held())
1488e4e1713SPravin B Shelar #define ovsl_dereference(p)					\
1498e4e1713SPravin B Shelar 	rcu_dereference_protected(p, lockdep_ovsl_is_held())
150663efa36SJesse Gross #define rcu_dereference_ovsl(p)					\
151663efa36SJesse Gross 	rcu_dereference_check(p, lockdep_ovsl_is_held())
1528e4e1713SPravin B Shelar 
15346df7b81SPravin B Shelar static inline struct net *ovs_dp_get_net(struct datapath *dp)
15446df7b81SPravin B Shelar {
15546df7b81SPravin B Shelar 	return read_pnet(&dp->net);
15646df7b81SPravin B Shelar }
15746df7b81SPravin B Shelar 
15846df7b81SPravin B Shelar static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
15946df7b81SPravin B Shelar {
16046df7b81SPravin B Shelar 	write_pnet(&dp->net, net);
16146df7b81SPravin B Shelar }
16246df7b81SPravin B Shelar 
1638e4e1713SPravin B Shelar struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
1648e4e1713SPravin B Shelar 
1658e4e1713SPravin B Shelar static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
1668e4e1713SPravin B Shelar {
1678e4e1713SPravin B Shelar 	WARN_ON_ONCE(!rcu_read_lock_held());
1688e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1698e4e1713SPravin B Shelar }
1708e4e1713SPravin B Shelar 
1718e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
1728e4e1713SPravin B Shelar {
1738e4e1713SPravin B Shelar 	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
1748e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1758e4e1713SPravin B Shelar }
1768e4e1713SPravin B Shelar 
1778e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
1788e4e1713SPravin B Shelar {
1798e4e1713SPravin B Shelar 	ASSERT_OVSL();
1808e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1818e4e1713SPravin B Shelar }
1828e4e1713SPravin B Shelar 
183ccb1352eSJesse Gross extern struct notifier_block ovs_dp_device_notifier;
18468eb5503SJohannes Berg extern struct genl_family dp_vport_genl_family;
185ccb1352eSJesse Gross 
186ccb1352eSJesse Gross void ovs_dp_process_received_packet(struct vport *, struct sk_buff *);
187ccb1352eSJesse Gross void ovs_dp_detach_port(struct vport *);
188ccb1352eSJesse Gross int ovs_dp_upcall(struct datapath *, struct sk_buff *,
189ccb1352eSJesse Gross 		  const struct dp_upcall_info *);
190ccb1352eSJesse Gross 
191ccb1352eSJesse Gross struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq,
192ccb1352eSJesse Gross 					 u8 cmd);
193ccb1352eSJesse Gross 
194ccb1352eSJesse Gross int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb);
1958e4e1713SPravin B Shelar void ovs_dp_notify_wq(struct work_struct *work);
19603f0d916SAndy Zhou 
19703f0d916SAndy Zhou #define OVS_NLERR(fmt, ...)					\
1981815a883SJoe Perches do {								\
1991815a883SJoe Perches 	if (net_ratelimit())					\
2001815a883SJoe Perches 		pr_info("netlink: " fmt, ##__VA_ARGS__);	\
2011815a883SJoe Perches } while (0)
202ccb1352eSJesse Gross #endif /* datapath.h */
203