xref: /openbmc/linux/net/openvswitch/datapath.h (revision aa310701)
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"
3046df7b81SPravin B Shelar #include "vport.h"
31ccb1352eSJesse Gross 
3215eac2a7SPravin B Shelar #define DP_MAX_PORTS           USHRT_MAX
3315eac2a7SPravin B Shelar #define DP_VPORT_HASH_BUCKETS  1024
3415eac2a7SPravin B Shelar 
35ccb1352eSJesse Gross #define SAMPLE_ACTION_DEPTH 3
36ccb1352eSJesse Gross 
37ccb1352eSJesse Gross /**
38ccb1352eSJesse Gross  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
39ccb1352eSJesse Gross  * datapath.
40ccb1352eSJesse Gross  * @n_hit: Number of received packets for which a matching flow was found in
41ccb1352eSJesse Gross  * the flow table.
42ccb1352eSJesse Gross  * @n_miss: Number of received packets that had no matching flow in the flow
43ccb1352eSJesse Gross  * table.  The sum of @n_hit and @n_miss is the number of packets that have
44ccb1352eSJesse Gross  * been received by the datapath.
45ccb1352eSJesse Gross  * @n_lost: Number of received packets that had no matching flow in the flow
46ccb1352eSJesse Gross  * table that could not be sent to userspace (normally due to an overflow in
47ccb1352eSJesse Gross  * one of the datapath's queues).
48ccb1352eSJesse Gross  */
49ccb1352eSJesse Gross struct dp_stats_percpu {
50ccb1352eSJesse Gross 	u64 n_hit;
51ccb1352eSJesse Gross 	u64 n_missed;
52ccb1352eSJesse Gross 	u64 n_lost;
53ccb1352eSJesse Gross 	struct u64_stats_sync sync;
54ccb1352eSJesse Gross };
55ccb1352eSJesse Gross 
56ccb1352eSJesse Gross /**
57ccb1352eSJesse Gross  * struct datapath - datapath for flow-based packet switching
58ccb1352eSJesse Gross  * @rcu: RCU callback head for deferred destruction.
59ccb1352eSJesse Gross  * @list_node: Element in global 'dps' list.
608e4e1713SPravin B Shelar  * @table: Current flow table.  Protected by ovs_mutex and RCU.
6115eac2a7SPravin B Shelar  * @ports: Hash table for ports.  %OVSP_LOCAL port always exists.  Protected by
628e4e1713SPravin B Shelar  * ovs_mutex and RCU.
63ccb1352eSJesse Gross  * @stats_percpu: Per-CPU datapath statistics.
6446df7b81SPravin B Shelar  * @net: Reference to net namespace.
65ccb1352eSJesse Gross  *
66ccb1352eSJesse Gross  * Context: See the comment on locking at the top of datapath.c for additional
67ccb1352eSJesse Gross  * locking information.
68ccb1352eSJesse Gross  */
69ccb1352eSJesse Gross struct datapath {
70ccb1352eSJesse Gross 	struct rcu_head rcu;
71ccb1352eSJesse Gross 	struct list_head list_node;
72ccb1352eSJesse Gross 
73ccb1352eSJesse Gross 	/* Flow table. */
74ccb1352eSJesse Gross 	struct flow_table __rcu *table;
75ccb1352eSJesse Gross 
76ccb1352eSJesse Gross 	/* Switch ports. */
7715eac2a7SPravin B Shelar 	struct hlist_head *ports;
78ccb1352eSJesse Gross 
79ccb1352eSJesse Gross 	/* Stats. */
80ccb1352eSJesse Gross 	struct dp_stats_percpu __percpu *stats_percpu;
8146df7b81SPravin B Shelar 
8246df7b81SPravin B Shelar #ifdef CONFIG_NET_NS
8346df7b81SPravin B Shelar 	/* Network namespace ref. */
8446df7b81SPravin B Shelar 	struct net *net;
8546df7b81SPravin B Shelar #endif
86ccb1352eSJesse Gross };
87ccb1352eSJesse Gross 
88ccb1352eSJesse Gross /**
89ccb1352eSJesse Gross  * struct ovs_skb_cb - OVS data in skb CB
90ccb1352eSJesse Gross  * @flow: The flow associated with this packet.  May be %NULL if no flow.
917d5437c7SPravin B Shelar  * @tun_key: Key for the tunnel that encapsulated this packet. NULL if the
927d5437c7SPravin B Shelar  * packet is not being tunneled.
93ccb1352eSJesse Gross  */
94ccb1352eSJesse Gross struct ovs_skb_cb {
95ccb1352eSJesse Gross 	struct sw_flow		*flow;
967d5437c7SPravin B Shelar 	struct ovs_key_ipv4_tunnel  *tun_key;
97ccb1352eSJesse Gross };
98ccb1352eSJesse Gross #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
99ccb1352eSJesse Gross 
100ccb1352eSJesse Gross /**
101ccb1352eSJesse Gross  * struct dp_upcall - metadata to include with a packet to send to userspace
102ccb1352eSJesse Gross  * @cmd: One of %OVS_PACKET_CMD_*.
103ccb1352eSJesse Gross  * @key: Becomes %OVS_PACKET_ATTR_KEY.  Must be nonnull.
1044490108bSBen Pfaff  * @userdata: If nonnull, its variable-length value is passed to userspace as
105ccb1352eSJesse Gross  * %OVS_PACKET_ATTR_USERDATA.
106ccb1352eSJesse Gross  * @pid: Netlink PID to which packet should be sent.  If @pid is 0 then no
107ccb1352eSJesse Gross  * packet is sent and the packet is accounted in the datapath's @n_lost
108ccb1352eSJesse Gross  * counter.
109ccb1352eSJesse Gross  */
110ccb1352eSJesse Gross struct dp_upcall_info {
111ccb1352eSJesse Gross 	u8 cmd;
112ccb1352eSJesse Gross 	const struct sw_flow_key *key;
113ccb1352eSJesse Gross 	const struct nlattr *userdata;
11415e47304SEric W. Biederman 	u32 portid;
115ccb1352eSJesse Gross };
116ccb1352eSJesse Gross 
1178e4e1713SPravin B Shelar /**
1188e4e1713SPravin B Shelar  * struct ovs_net - Per net-namespace data for ovs.
1198e4e1713SPravin B Shelar  * @dps: List of datapaths to enable dumping them all out.
1208e4e1713SPravin B Shelar  * Protected by genl_mutex.
1218e4e1713SPravin B Shelar  */
1228e4e1713SPravin B Shelar struct ovs_net {
1238e4e1713SPravin B Shelar 	struct list_head dps;
1248e4e1713SPravin B Shelar 	struct work_struct dp_notify_work;
125aa310701SPravin B Shelar 	struct vport_net vport_net;
1268e4e1713SPravin B Shelar };
1278e4e1713SPravin B Shelar 
1288e4e1713SPravin B Shelar extern int ovs_net_id;
1298e4e1713SPravin B Shelar void ovs_lock(void);
1308e4e1713SPravin B Shelar void ovs_unlock(void);
1318e4e1713SPravin B Shelar 
1328e4e1713SPravin B Shelar #ifdef CONFIG_LOCKDEP
1338e4e1713SPravin B Shelar int lockdep_ovsl_is_held(void);
1348e4e1713SPravin B Shelar #else
1358e4e1713SPravin B Shelar #define lockdep_ovsl_is_held()	1
1368e4e1713SPravin B Shelar #endif
1378e4e1713SPravin B Shelar 
1388e4e1713SPravin B Shelar #define ASSERT_OVSL()		WARN_ON(unlikely(!lockdep_ovsl_is_held()))
1398e4e1713SPravin B Shelar #define ovsl_dereference(p)					\
1408e4e1713SPravin B Shelar 	rcu_dereference_protected(p, lockdep_ovsl_is_held())
1418e4e1713SPravin B Shelar 
14246df7b81SPravin B Shelar static inline struct net *ovs_dp_get_net(struct datapath *dp)
14346df7b81SPravin B Shelar {
14446df7b81SPravin B Shelar 	return read_pnet(&dp->net);
14546df7b81SPravin B Shelar }
14646df7b81SPravin B Shelar 
14746df7b81SPravin B Shelar static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
14846df7b81SPravin B Shelar {
14946df7b81SPravin B Shelar 	write_pnet(&dp->net, net);
15046df7b81SPravin B Shelar }
15146df7b81SPravin B Shelar 
1528e4e1713SPravin B Shelar struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
1538e4e1713SPravin B Shelar 
1548e4e1713SPravin B Shelar static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
1558e4e1713SPravin B Shelar {
1568e4e1713SPravin B Shelar 	WARN_ON_ONCE(!rcu_read_lock_held());
1578e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1588e4e1713SPravin B Shelar }
1598e4e1713SPravin B Shelar 
1608e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
1618e4e1713SPravin B Shelar {
1628e4e1713SPravin B Shelar 	WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
1638e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1648e4e1713SPravin B Shelar }
1658e4e1713SPravin B Shelar 
1668e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
1678e4e1713SPravin B Shelar {
1688e4e1713SPravin B Shelar 	ASSERT_OVSL();
1698e4e1713SPravin B Shelar 	return ovs_lookup_vport(dp, port_no);
1708e4e1713SPravin B Shelar }
1718e4e1713SPravin B Shelar 
172ccb1352eSJesse Gross extern struct notifier_block ovs_dp_device_notifier;
173ccb1352eSJesse Gross extern struct genl_multicast_group ovs_dp_vport_multicast_group;
174ccb1352eSJesse Gross 
175ccb1352eSJesse Gross void ovs_dp_process_received_packet(struct vport *, struct sk_buff *);
176ccb1352eSJesse Gross void ovs_dp_detach_port(struct vport *);
177ccb1352eSJesse Gross int ovs_dp_upcall(struct datapath *, struct sk_buff *,
178ccb1352eSJesse Gross 		  const struct dp_upcall_info *);
179ccb1352eSJesse Gross 
180ccb1352eSJesse Gross const char *ovs_dp_name(const struct datapath *dp);
181ccb1352eSJesse Gross struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq,
182ccb1352eSJesse Gross 					 u8 cmd);
183ccb1352eSJesse Gross 
184ccb1352eSJesse Gross int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb);
1858e4e1713SPravin B Shelar void ovs_dp_notify_wq(struct work_struct *work);
186ccb1352eSJesse Gross #endif /* datapath.h */
187