xref: /openbmc/linux/net/openvswitch/datapath.h (revision caf2ee14)
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"
30ccb1352eSJesse Gross 
31ccb1352eSJesse Gross struct vport;
32ccb1352eSJesse Gross 
33ccb1352eSJesse Gross #define DP_MAX_PORTS 1024
34ccb1352eSJesse Gross #define SAMPLE_ACTION_DEPTH 3
35ccb1352eSJesse Gross 
36ccb1352eSJesse Gross /**
37ccb1352eSJesse Gross  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
38ccb1352eSJesse Gross  * datapath.
39ccb1352eSJesse Gross  * @n_hit: Number of received packets for which a matching flow was found in
40ccb1352eSJesse Gross  * the flow table.
41ccb1352eSJesse Gross  * @n_miss: Number of received packets that had no matching flow in the flow
42ccb1352eSJesse Gross  * table.  The sum of @n_hit and @n_miss is the number of packets that have
43ccb1352eSJesse Gross  * been received by the datapath.
44ccb1352eSJesse Gross  * @n_lost: Number of received packets that had no matching flow in the flow
45ccb1352eSJesse Gross  * table that could not be sent to userspace (normally due to an overflow in
46ccb1352eSJesse Gross  * one of the datapath's queues).
47ccb1352eSJesse Gross  */
48ccb1352eSJesse Gross struct dp_stats_percpu {
49ccb1352eSJesse Gross 	u64 n_hit;
50ccb1352eSJesse Gross 	u64 n_missed;
51ccb1352eSJesse Gross 	u64 n_lost;
52ccb1352eSJesse Gross 	struct u64_stats_sync sync;
53ccb1352eSJesse Gross };
54ccb1352eSJesse Gross 
55ccb1352eSJesse Gross /**
56ccb1352eSJesse Gross  * struct datapath - datapath for flow-based packet switching
57ccb1352eSJesse Gross  * @rcu: RCU callback head for deferred destruction.
58ccb1352eSJesse Gross  * @list_node: Element in global 'dps' list.
59ccb1352eSJesse Gross  * @n_flows: Number of flows currently in flow table.
60ccb1352eSJesse Gross  * @table: Current flow table.  Protected by genl_lock and RCU.
61ccb1352eSJesse Gross  * @ports: Map from port number to &struct vport.  %OVSP_LOCAL port
62ccb1352eSJesse Gross  * always exists, other ports may be %NULL.  Protected by RTNL and RCU.
63ccb1352eSJesse Gross  * @port_list: List of all ports in @ports in arbitrary order.  RTNL required
64ccb1352eSJesse Gross  * to iterate or modify.
65ccb1352eSJesse Gross  * @stats_percpu: Per-CPU datapath statistics.
66ccb1352eSJesse Gross  *
67ccb1352eSJesse Gross  * Context: See the comment on locking at the top of datapath.c for additional
68ccb1352eSJesse Gross  * locking information.
69ccb1352eSJesse Gross  */
70ccb1352eSJesse Gross struct datapath {
71ccb1352eSJesse Gross 	struct rcu_head rcu;
72ccb1352eSJesse Gross 	struct list_head list_node;
73ccb1352eSJesse Gross 
74ccb1352eSJesse Gross 	/* Flow table. */
75ccb1352eSJesse Gross 	struct flow_table __rcu *table;
76ccb1352eSJesse Gross 
77ccb1352eSJesse Gross 	/* Switch ports. */
78ccb1352eSJesse Gross 	struct vport __rcu *ports[DP_MAX_PORTS];
79ccb1352eSJesse Gross 	struct list_head port_list;
80ccb1352eSJesse Gross 
81ccb1352eSJesse Gross 	/* Stats. */
82ccb1352eSJesse Gross 	struct dp_stats_percpu __percpu *stats_percpu;
83ccb1352eSJesse Gross };
84ccb1352eSJesse Gross 
85ccb1352eSJesse Gross /**
86ccb1352eSJesse Gross  * struct ovs_skb_cb - OVS data in skb CB
87ccb1352eSJesse Gross  * @flow: The flow associated with this packet.  May be %NULL if no flow.
88ccb1352eSJesse Gross  */
89ccb1352eSJesse Gross struct ovs_skb_cb {
90ccb1352eSJesse Gross 	struct sw_flow		*flow;
91ccb1352eSJesse Gross };
92ccb1352eSJesse Gross #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
93ccb1352eSJesse Gross 
94ccb1352eSJesse Gross /**
95ccb1352eSJesse Gross  * struct dp_upcall - metadata to include with a packet to send to userspace
96ccb1352eSJesse Gross  * @cmd: One of %OVS_PACKET_CMD_*.
97ccb1352eSJesse Gross  * @key: Becomes %OVS_PACKET_ATTR_KEY.  Must be nonnull.
98ccb1352eSJesse Gross  * @userdata: If nonnull, its u64 value is extracted and passed to userspace as
99ccb1352eSJesse Gross  * %OVS_PACKET_ATTR_USERDATA.
100ccb1352eSJesse Gross  * @pid: Netlink PID to which packet should be sent.  If @pid is 0 then no
101ccb1352eSJesse Gross  * packet is sent and the packet is accounted in the datapath's @n_lost
102ccb1352eSJesse Gross  * counter.
103ccb1352eSJesse Gross  */
104ccb1352eSJesse Gross struct dp_upcall_info {
105ccb1352eSJesse Gross 	u8 cmd;
106ccb1352eSJesse Gross 	const struct sw_flow_key *key;
107ccb1352eSJesse Gross 	const struct nlattr *userdata;
108ccb1352eSJesse Gross 	u32 pid;
109ccb1352eSJesse Gross };
110ccb1352eSJesse Gross 
111ccb1352eSJesse Gross extern struct notifier_block ovs_dp_device_notifier;
112ccb1352eSJesse Gross extern struct genl_multicast_group ovs_dp_vport_multicast_group;
113ccb1352eSJesse Gross 
114ccb1352eSJesse Gross void ovs_dp_process_received_packet(struct vport *, struct sk_buff *);
115ccb1352eSJesse Gross void ovs_dp_detach_port(struct vport *);
116ccb1352eSJesse Gross int ovs_dp_upcall(struct datapath *, struct sk_buff *,
117ccb1352eSJesse Gross 		  const struct dp_upcall_info *);
118ccb1352eSJesse Gross 
119ccb1352eSJesse Gross const char *ovs_dp_name(const struct datapath *dp);
120ccb1352eSJesse Gross struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq,
121ccb1352eSJesse Gross 					 u8 cmd);
122ccb1352eSJesse Gross 
123ccb1352eSJesse Gross int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb);
124ccb1352eSJesse Gross #endif /* datapath.h */
125