datapath: Fully initialize datapath before local port.
[sliver-openvswitch.git] / datapath / datapath.h
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 /* Interface exported by openvswitch_mod. */
10
11 #ifndef DATAPATH_H
12 #define DATAPATH_H 1
13
14 #include <asm/page.h>
15 #include <linux/kernel.h>
16 #include <linux/mutex.h>
17 #include <linux/netdevice.h>
18 #include <linux/seqlock.h>
19 #include <linux/skbuff.h>
20 #include <linux/version.h>
21
22 #include "checksum.h"
23 #include "compat.h"
24 #include "flow.h"
25 #include "dp_sysfs.h"
26 #include "vlan.h"
27
28 struct vport;
29
30 /* Mask for the priority bits in a vlan header.  If we ever merge upstream
31  * then this should go into include/linux/if_vlan.h. */
32 #define VLAN_PCP_MASK 0xe000
33 #define VLAN_PCP_SHIFT 13
34
35 #define DP_MAX_PORTS 1024
36
37 /**
38  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
39  * datapath.
40  * @n_frags: Number of IP fragments processed by datapath.
41  * @n_hit: Number of received packets for which a matching flow was found in
42  * the flow table.
43  * @n_miss: Number of received packets that had no matching flow in the flow
44  * table.  The sum of @n_hit and @n_miss is the number of packets that have
45  * been received by the datapath.
46  * @n_lost: Number of received packets that had no matching flow in the flow
47  * table that could not be sent to userspace (normally due to an overflow in
48  * one of the datapath's queues).
49  */
50 struct dp_stats_percpu {
51         u64 n_frags;
52         u64 n_hit;
53         u64 n_missed;
54         u64 n_lost;
55         seqcount_t seqlock;
56 };
57
58 /**
59  * struct datapath - datapath for flow-based packet switching
60  * @rcu: RCU callback head for deferred destruction.
61  * @list_node: Element in global 'dps' list.
62  * @ifobj: Represents /sys/class/net/<devname>/brif.  Protected by RTNL.
63  * @drop_frags: Drop all IP fragments if nonzero.
64  * @n_flows: Number of flows currently in flow table.
65  * @table: Current flow table.  Protected by genl_lock and RCU.
66  * @ports: Map from port number to &struct vport.  %OVSP_LOCAL port
67  * always exists, other ports may be %NULL.  Protected by RTNL and RCU.
68  * @port_list: List of all ports in @ports in arbitrary order.  RTNL required
69  * to iterate or modify.
70  * @stats_percpu: Per-CPU datapath statistics.
71  * @sflow_probability: Number of packets out of UINT_MAX to sample to the
72  * %OVS_PACKET_CMD_SAMPLE multicast group, e.g. (@sflow_probability/UINT_MAX)
73  * is the probability of sampling a given packet.
74  *
75  * Context: See the comment on locking at the top of datapath.c for additional
76  * locking information.
77  */
78 struct datapath {
79         struct rcu_head rcu;
80         struct list_head list_node;
81         struct kobject ifobj;
82
83         int drop_frags;
84
85         /* Flow table. */
86         struct flow_table __rcu *table;
87
88         /* Switch ports. */
89         struct vport __rcu *ports[DP_MAX_PORTS];
90         struct list_head port_list;
91
92         /* Stats. */
93         struct dp_stats_percpu __percpu *stats_percpu;
94
95         /* sFlow Sampling */
96         unsigned int sflow_probability;
97 };
98
99 /**
100  * struct ovs_skb_cb - OVS data in skb CB
101  * @vport: The datapath port on which the skb entered the switch.
102  * @flow: The flow associated with this packet.  May be %NULL if no flow.
103  * @tun_id: ID of the tunnel that encapsulated this packet.  It is 0 if the
104  * @ip_summed: Consistently stores L4 checksumming status across different
105  * kernel versions.
106  * @csum_start: Stores the offset from which to start checksumming independent
107  * of the transport header on all kernel versions.
108  * packet was not received on a tunnel.
109  * @vlan_tci: Provides a substitute for the skb->vlan_tci field on kernels
110  * before 2.6.27.
111  */
112 struct ovs_skb_cb {
113         struct vport            *vport;
114         struct sw_flow          *flow;
115         __be64                  tun_id;
116 #ifdef NEED_CSUM_NORMALIZE
117         enum csum_type          ip_summed;
118         u16                     csum_start;
119 #endif
120 #ifdef NEED_VLAN_FIELD
121         u16                     vlan_tci;
122 #endif
123 };
124 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
125
126 /**
127  * struct dp_upcall - metadata to include with a packet to send to userspace
128  * @cmd: One of %OVS_PACKET_CMD_*.
129  * @key: Becomes %OVS_PACKET_ATTR_KEY.  Must be nonnull.
130  * @userdata: Becomes %OVS_PACKET_ATTR_USERDATA if nonzero.
131  * @sample_pool: Becomes %OVS_PACKET_ATTR_SAMPLE_POOL if nonzero.
132  * @actions: Becomes %OVS_PACKET_ATTR_ACTIONS if nonnull.
133  * @actions_len: Number of bytes in @actions.
134 */
135 struct dp_upcall_info {
136         u8 cmd;
137         const struct sw_flow_key *key;
138         u64 userdata;
139         u32 sample_pool;
140         const struct nlattr *actions;
141         u32 actions_len;
142 };
143
144 extern struct notifier_block dp_device_notifier;
145 extern struct genl_multicast_group dp_vport_multicast_group;
146 extern int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
147
148 void dp_process_received_packet(struct vport *, struct sk_buff *);
149 void dp_detach_port(struct vport *);
150 int dp_upcall(struct datapath *, struct sk_buff *, const struct dp_upcall_info *);
151
152 struct datapath *get_dp(int dp_idx);
153 const char *dp_name(const struct datapath *dp);
154 struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq,
155                                          u8 cmd);
156
157 #endif /* datapath.h */