xref: /openbmc/linux/net/openvswitch/datapath.h (revision 68eb5503)
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;
58ccb1352eSJesse Gross 	struct u64_stats_sync sync;
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
91ccb1352eSJesse Gross };
92ccb1352eSJesse Gross 
93ccb1352eSJesse Gross /**
94ccb1352eSJesse Gross  * struct ovs_skb_cb - OVS data in skb CB
95ccb1352eSJesse Gross  * @flow: The flow associated with this packet.  May be %NULL if no flow.
9603f0d916SAndy Zhou  * @pkt_key: The flow information extracted from the packet.  Must be nonnull.
977d5437c7SPravin B Shelar  * @tun_key: Key for the tunnel that encapsulated this packet. NULL if the
987d5437c7SPravin B Shelar  * packet is not being tunneled.
99ccb1352eSJesse Gross  */
100ccb1352eSJesse Gross struct ovs_skb_cb {
101ccb1352eSJesse Gross 	struct sw_flow		*flow;
10203f0d916SAndy Zhou 	struct sw_flow_key	*pkt_key;
1037d5437c7SPravin B Shelar 	struct ovs_key_ipv4_tunnel  *tun_key;
104ccb1352eSJesse Gross };
105ccb1352eSJesse Gross #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
106ccb1352eSJesse Gross 
107ccb1352eSJesse Gross /**
108ccb1352eSJesse Gross  * struct dp_upcall - metadata to include with a packet to send to userspace
109ccb1352eSJesse Gross  * @cmd: One of %OVS_PACKET_CMD_*.
110ccb1352eSJesse Gross  * @key: Becomes %OVS_PACKET_ATTR_KEY.  Must be nonnull.
1114490108bSBen Pfaff  * @userdata: If nonnull, its variable-length value is passed to userspace as
112ccb1352eSJesse Gross  * %OVS_PACKET_ATTR_USERDATA.
113ccb1352eSJesse Gross  * @pid: Netlink PID to which packet should be sent.  If @pid is 0 then no
114ccb1352eSJesse Gross  * packet is sent and the packet is accounted in the datapath's @n_lost
115ccb1352eSJesse Gross  * counter.
116ccb1352eSJesse Gross  */
117ccb1352eSJesse Gross struct dp_upcall_info {
118ccb1352eSJesse Gross 	u8 cmd;
119ccb1352eSJesse Gross 	const struct sw_flow_key *key;
120ccb1352eSJesse Gross 	const struct nlattr *userdata;
12115e47304SEric W. Biederman 	u32 portid;
122ccb1352eSJesse Gross };
123ccb1352eSJesse Gross 
1248e4e1713SPravin B Shelar /**
1258e4e1713SPravin B Shelar  * struct ovs_net - Per net-namespace data for ovs.
1268e4e1713SPravin B Shelar  * @dps: List of datapaths to enable dumping them all out.
1278e4e1713SPravin B Shelar  * Protected by genl_mutex.
1288e4e1713SPravin B Shelar  */
1298e4e1713SPravin B Shelar struct ovs_net {
1308e4e1713SPravin B Shelar 	struct list_head dps;
1318e4e1713SPravin B Shelar 	struct work_struct dp_notify_work;
132aa310701SPravin B Shelar 	struct vport_net vport_net;
1338e4e1713SPravin B Shelar };
1348e4e1713SPravin B Shelar 
1358e4e1713SPravin B Shelar extern int ovs_net_id;
1368e4e1713SPravin B Shelar void ovs_lock(void);
1378e4e1713SPravin B Shelar void ovs_unlock(void);
1388e4e1713SPravin B Shelar 
1398e4e1713SPravin B Shelar #ifdef CONFIG_LOCKDEP
1408e4e1713SPravin B Shelar int lockdep_ovsl_is_held(void);
1418e4e1713SPravin B Shelar #else
1428e4e1713SPravin B Shelar #define lockdep_ovsl_is_held()	1
1438e4e1713SPravin B Shelar #endif
1448e4e1713SPravin B Shelar 
1458e4e1713SPravin B Shelar #define ASSERT_OVSL()		WARN_ON(unlikely(!lockdep_ovsl_is_held()))
1468e4e1713SPravin B Shelar #define ovsl_dereference(p)					\
1478e4e1713SPravin B Shelar 	rcu_dereference_protected(p, lockdep_ovsl_is_held())
1488e4e1713SPravin B Shelar 
14946df7b81SPravin B Shelar static inline struct net *ovs_dp_get_net(struct datapath *dp)
15046df7b81SPravin B Shelar {
15146df7b81SPravin B Shelar 	return read_pnet(&dp->net);
15246df7b81SPravin B Shelar }
15346df7b81SPravin B Shelar 
15446df7b81SPravin B Shelar static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
15546df7b81SPravin B Shelar {
15646df7b81SPravin B Shelar 	write_pnet(&dp->net, net);
15746df7b81SPravin B Shelar }
15846df7b81SPravin B Shelar 
1598e4e1713SPravin B Shelar struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
1608e4e1713SPravin B Shelar 
1618e4e1713SPravin B Shelar static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
1628e4e1713SPravin B Shelar {
1638e4e1713SPravin B Shelar 	WARN_ON_ONCE(!rcu_read_lock_held());
1648e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1658e4e1713SPravin B Shelar }
1668e4e1713SPravin B Shelar 
1678e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
1688e4e1713SPravin B Shelar {
1698e4e1713SPravin B Shelar 	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
1708e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1718e4e1713SPravin B Shelar }
1728e4e1713SPravin B Shelar 
1738e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
1748e4e1713SPravin B Shelar {
1758e4e1713SPravin B Shelar 	ASSERT_OVSL();
1768e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1778e4e1713SPravin B Shelar }
1788e4e1713SPravin B Shelar 
179ccb1352eSJesse Gross extern struct notifier_block ovs_dp_device_notifier;
18068eb5503SJohannes Berg extern struct genl_family dp_vport_genl_family;
181ccb1352eSJesse Gross extern struct genl_multicast_group ovs_dp_vport_multicast_group;
182ccb1352eSJesse Gross 
183ccb1352eSJesse Gross void ovs_dp_process_received_packet(struct vport *, struct sk_buff *);
184ccb1352eSJesse Gross void ovs_dp_detach_port(struct vport *);
185ccb1352eSJesse Gross int ovs_dp_upcall(struct datapath *, struct sk_buff *,
186ccb1352eSJesse Gross 		  const struct dp_upcall_info *);
187ccb1352eSJesse Gross 
188ccb1352eSJesse Gross const char *ovs_dp_name(const struct datapath *dp);
189ccb1352eSJesse Gross struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq,
190ccb1352eSJesse Gross 					 u8 cmd);
191ccb1352eSJesse Gross 
192ccb1352eSJesse Gross int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb);
1938e4e1713SPravin B Shelar void ovs_dp_notify_wq(struct work_struct *work);
19403f0d916SAndy Zhou 
19503f0d916SAndy Zhou #define OVS_NLERR(fmt, ...) \
19603f0d916SAndy Zhou 	pr_info_once("netlink: " fmt, ##__VA_ARGS__)
19703f0d916SAndy Zhou 
198ccb1352eSJesse Gross #endif /* datapath.h */
199