2 * Copyright (c) 2009, 2010 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 /* Interface exported by openvswitch_mod. */
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>
24 /* Mask for the priority bits in a vlan header. If we ever merge upstream
25 * then this should go into include/linux/if_vlan.h. */
26 #define VLAN_PCP_MASK 0xe000
27 #define VLAN_PCP_SHIFT 13
29 #define DP_MAX_PORTS 1024
30 #define DP_MAX_GROUPS 16
32 #define DP_L2_BITS (PAGE_SHIFT - ilog2(sizeof(struct dp_bucket*)))
33 #define DP_L2_SIZE (1 << DP_L2_BITS)
36 #define DP_L1_BITS (PAGE_SHIFT - ilog2(sizeof(struct dp_bucket**)))
37 #define DP_L1_SIZE (1 << DP_L1_BITS)
38 #define DP_L1_SHIFT DP_L2_BITS
40 /* For 4 kB pages, this is 1,048,576 on 32-bit or 262,144 on 64-bit. */
41 #define DP_MAX_BUCKETS (DP_L1_SIZE * DP_L2_SIZE)
44 * struct dp_table - flow table
45 * @n_buckets: number of buckets (a power of 2 between %DP_L1_SIZE and
47 * @buckets: pointer to @n_buckets/%DP_L1_SIZE pointers to %DP_L1_SIZE pointers
49 * @hash_seed: random number used for flow hashing, to make the hash
50 * distribution harder to predict
51 * @rcu: RCU callback structure
53 * The @buckets array is logically an array of pointers to buckets. It is
54 * broken into two levels to avoid the need to kmalloc() any object larger than
55 * a single page or to use vmalloc(). @buckets is always nonnull, as is each
56 * @buckets[i], but each @buckets[i][j] is nonnull only if the specified hash
57 * bucket is nonempty (for 0 <= i < @n_buckets/%DP_L1_SIZE, 0 <= j <
61 unsigned int n_buckets;
62 struct dp_bucket ***buckets;
63 unsigned int hash_seed;
68 * struct dp_bucket - single bucket within datapath flow table
69 * @rcu: RCU callback structure
70 * @n_flows: number of flows in @flows[] array
71 * @flows: array of @n_flows pointers to flows
73 * The expected number of flows per bucket is 1, but this allows for an
74 * arbitrary number of collisions.
79 struct sw_flow *flows[];
83 #define DP_MAX_QUEUE_LEN 100
86 * struct dp_stats_percpu - per-cpu packet processing statistics for a given
88 * @n_frags: Number of IP fragments processed by datapath.
89 * @n_hit: Number of received packets for which a matching flow was found in
91 * @n_miss: Number of received packets that had no matching flow in the flow
92 * table. The sum of @n_hit and @n_miss is the number of packets that have
93 * been received by the datapath.
94 * @n_lost: Number of received packets that had no matching flow in the flow
95 * table that could not be sent to userspace (normally due to an overflow in
96 * one of the datapath's queues).
98 struct dp_stats_percpu {
105 struct dp_port_group {
112 * struct datapath - datapath for flow-based packet switching
113 * @mutex: Mutual exclusion for ioctls.
114 * @dp_idx: Datapath number (index into the dps[] array in datapath.c).
115 * @ifobj: Represents /sys/class/net/<devname>/brif.
116 * @drop_frags: Drop all IP fragments if nonzero.
117 * @queues: %DP_N_QUEUES sets of queued packets for userspace to handle.
118 * @waitqueue: Waitqueue, for waiting for new packets in @queues.
119 * @n_flows: Number of flows currently in flow table.
120 * @table: Current flow table (RCU protected).
121 * @groups: Port groups, used by ODPAT_OUTPUT_GROUP action (RCU protected).
122 * @n_ports: Number of ports currently in @ports.
123 * @ports: Map from port number to &struct net_bridge_port. %ODPP_LOCAL port
124 * always exists, other ports may be %NULL.
125 * @port_list: List of all ports in @ports in arbitrary order.
126 * @stats_percpu: Per-CPU datapath statistics.
127 * @sflow_probability: Number of packets out of UINT_MAX to sample to the
128 * %ODPL_SFLOW queue, e.g. (@sflow_probability/UINT_MAX) is the probability of
129 * sampling a given packet.
134 struct kobject ifobj;
139 struct sk_buff_head queues[DP_N_QUEUES];
140 wait_queue_head_t waitqueue;
143 unsigned int n_flows;
144 struct dp_table *table;
147 struct dp_port_group *groups[DP_MAX_GROUPS];
150 unsigned int n_ports;
151 struct net_bridge_port *ports[DP_MAX_PORTS];
152 struct list_head port_list;
155 struct dp_stats_percpu *stats_percpu;
158 unsigned int sflow_probability;
162 * struct net_bridge_port - one port within a datapath
163 * @port_no: Index into @dp's @ports array.
164 * @dp: Datapath to which this port belongs.
165 * @dev: The network device attached to this port. The @br_port member in @dev
166 * points back to this &struct net_bridge_port.
167 * @kobj: Represents /sys/class/net/<devname>/brport.
168 * @linkname: The name of the link from /sys/class/net/<datapath>/brif to this
169 * &struct net_bridge_port. (We keep this around so that we can delete it
170 * if @dev gets renamed.) Set to the null string when no link exists.
171 * @node: Element in @dp's @port_list.
172 * @sflow_pool: Number of packets that were candidates for sFlow sampling,
173 * regardless of whether they were actually chosen and sent down to userspace.
175 struct net_bridge_port {
178 struct net_device *dev;
180 char linkname[IFNAMSIZ];
181 struct list_head node;
187 CSUM_UNNECESSARY = 1,
193 * struct ovs_skb_cb - OVS data in skb CB
194 * @ip_summed: Consistently stores L4 checksumming status across different
198 enum csum_type ip_summed;
200 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
202 extern struct notifier_block dp_device_notifier;
203 extern int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
206 struct dp_table *dp_table_create(unsigned int n_buckets);
207 void dp_table_destroy(struct dp_table *, int free_flows);
208 struct sw_flow *dp_table_lookup(struct dp_table *, const struct odp_flow_key *);
209 int dp_table_insert(struct dp_table *, struct sw_flow *);
210 int dp_table_delete(struct dp_table *, struct sw_flow *);
211 int dp_table_expand(struct datapath *);
212 int dp_table_flush(struct datapath *);
213 int dp_table_foreach(struct dp_table *table,
214 int (*callback)(struct sw_flow *flow, void *aux),
217 void dp_process_received_packet(struct sk_buff *, struct net_bridge_port *);
218 int dp_del_port(struct net_bridge_port *);
219 int dp_output_control(struct datapath *, struct sk_buff *, int, u32 arg);
220 int dp_min_mtu(const struct datapath *dp);
221 void set_dp_devs_mtu(const struct datapath *dp, struct net_device *dev);
223 struct datapath *get_dp(int dp_idx);
225 static inline const char *dp_name(const struct datapath *dp)
227 return dp->ports[ODPP_LOCAL]->dev->name;
230 #if defined(CONFIG_XEN) && defined(HAVE_PROTO_DATA_VALID)
231 int vswitch_skb_checksum_setup(struct sk_buff *skb);
233 static inline int vswitch_skb_checksum_setup(struct sk_buff *skb)
239 void forward_ip_summed(struct sk_buff *skb);
241 #endif /* datapath.h */