datapath: Genericize hash table.
[sliver-openvswitch.git] / datapath / datapath.h
1 /*
2  * Copyright (c) 2009, 2010 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/workqueue.h>
19 #include <linux/skbuff.h>
20 #include <linux/version.h>
21 #include "flow.h"
22 #include "dp_sysfs.h"
23
24 struct vport;
25 struct dp_port;
26
27 /* Mask for the priority bits in a vlan header.  If we ever merge upstream
28  * then this should go into include/linux/if_vlan.h. */
29 #define VLAN_PCP_MASK 0xe000
30 #define VLAN_PCP_SHIFT 13
31
32 #define DP_MAX_PORTS 1024
33 #define DP_MAX_GROUPS 16
34
35 #define DP_N_QUEUES 3
36 #define DP_MAX_QUEUE_LEN 100
37
38 /**
39  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
40  * datapath.
41  * @n_frags: Number of IP fragments processed by datapath.
42  * @n_hit: Number of received packets for which a matching flow was found in
43  * the flow table.
44  * @n_miss: Number of received packets that had no matching flow in the flow
45  * table.  The sum of @n_hit and @n_miss is the number of packets that have
46  * been received by the datapath.
47  * @n_lost: Number of received packets that had no matching flow in the flow
48  * table that could not be sent to userspace (normally due to an overflow in
49  * one of the datapath's queues).
50  */
51 struct dp_stats_percpu {
52         u64 n_frags;
53         u64 n_hit;
54         u64 n_missed;
55         u64 n_lost;
56 };
57
58 struct dp_port_group {
59         struct rcu_head rcu;
60         int n_ports;
61         u16 ports[];
62 };
63
64 /**
65  * struct datapath - datapath for flow-based packet switching
66  * @mutex: Mutual exclusion for ioctls.
67  * @dp_idx: Datapath number (index into the dps[] array in datapath.c).
68  * @ifobj: Represents /sys/class/net/<devname>/brif.
69  * @drop_frags: Drop all IP fragments if nonzero.
70  * @queues: %DP_N_QUEUES sets of queued packets for userspace to handle.
71  * @waitqueue: Waitqueue, for waiting for new packets in @queues.
72  * @n_flows: Number of flows currently in flow table.
73  * @table: Current flow table (RCU protected).
74  * @groups: Port groups, used by ODPAT_OUTPUT_GROUP action (RCU protected).
75  * @n_ports: Number of ports currently in @ports.
76  * @ports: Map from port number to &struct dp_port.  %ODPP_LOCAL port
77  * always exists, other ports may be %NULL.
78  * @port_list: List of all ports in @ports in arbitrary order.
79  * @stats_percpu: Per-CPU datapath statistics.
80  * @sflow_probability: Number of packets out of UINT_MAX to sample to the
81  * %ODPL_SFLOW queue, e.g. (@sflow_probability/UINT_MAX) is the probability of
82  * sampling a given packet.
83  */
84 struct datapath {
85         struct mutex mutex;
86         int dp_idx;
87         struct kobject ifobj;
88
89         int drop_frags;
90
91         /* Queued data. */
92         struct sk_buff_head queues[DP_N_QUEUES];
93         wait_queue_head_t waitqueue;
94
95         /* Flow table. */
96         struct tbl *table;
97
98         /* Port groups. */
99         struct dp_port_group *groups[DP_MAX_GROUPS];
100
101         /* Switch ports. */
102         unsigned int n_ports;
103         struct dp_port *ports[DP_MAX_PORTS];
104         struct list_head port_list;
105
106         /* Stats. */
107         struct dp_stats_percpu *stats_percpu;
108
109         /* sFlow Sampling */
110         unsigned int sflow_probability;
111 };
112
113 /**
114  * struct dp_port - one port within a datapath
115  * @port_no: Index into @dp's @ports array.
116  * @dp: Datapath to which this port belongs.
117  * @vport: The network device attached to this port.  The contents depends on
118  * the device and should be accessed only through the vport_* functions.
119  * @kobj: Represents /sys/class/net/<devname>/brport.
120  * @linkname: The name of the link from /sys/class/net/<datapath>/brif to this
121  * &struct dp_port.  (We keep this around so that we can delete it if the
122  * device gets renamed.)  Set to the null string when no link exists.
123  * @node: Element in @dp's @port_list.
124  * @sflow_pool: Number of packets that were candidates for sFlow sampling,
125  * regardless of whether they were actually chosen and sent down to userspace.
126  */
127 struct dp_port {
128         u16 port_no;
129         struct datapath *dp;
130         struct vport *vport;
131         struct kobject kobj;
132         char linkname[IFNAMSIZ];
133         struct list_head node;
134         atomic_t sflow_pool;
135 };
136
137 enum csum_type {
138         OVS_CSUM_NONE = 0,
139         OVS_CSUM_UNNECESSARY = 1,
140         OVS_CSUM_COMPLETE = 2,
141         OVS_CSUM_PARTIAL = 3,
142 };
143
144 /**
145  * struct ovs_skb_cb - OVS data in skb CB
146  * @br_port: The bridge port on which the skb entered the switch.
147  * @ip_summed: Consistently stores L4 checksumming status across different
148  * kernel versions.
149  */
150 struct ovs_skb_cb {
151         struct dp_port          *dp_port;
152         enum csum_type          ip_summed;
153         __be32                  tun_id;
154 };
155 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
156
157 extern struct notifier_block dp_device_notifier;
158 extern int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
159
160 void dp_process_received_packet(struct dp_port *, struct sk_buff *);
161 int dp_detach_port(struct dp_port *, int may_delete);
162 int dp_output_control(struct datapath *, struct sk_buff *, int, u32 arg);
163 int dp_min_mtu(const struct datapath *dp);
164 void set_internal_devs_mtu(const struct datapath *dp);
165
166 struct datapath *get_dp(int dp_idx);
167 const char *dp_name(const struct datapath *dp);
168
169 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
170 int vswitch_skb_checksum_setup(struct sk_buff *skb);
171 #else
172 static inline int vswitch_skb_checksum_setup(struct sk_buff *skb)
173 {
174         return 0;
175 }
176 #endif
177
178 void compute_ip_summed(struct sk_buff *skb, bool xmit);
179 void forward_ip_summed(struct sk_buff *skb);
180
181 #endif /* datapath.h */