datapath: optimize flow compare and mask functions
[sliver-openvswitch.git] / datapath / flow.h
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #ifndef FLOW_H
20 #define FLOW_H 1
21
22 #include <linux/kernel.h>
23 #include <linux/netlink.h>
24 #include <linux/openvswitch.h>
25 #include <linux/spinlock.h>
26 #include <linux/types.h>
27 #include <linux/rcupdate.h>
28 #include <linux/if_ether.h>
29 #include <linux/in6.h>
30 #include <linux/jiffies.h>
31 #include <linux/time.h>
32 #include <linux/flex_array.h>
33
34 #include <net/inet_ecn.h>
35 #include <net/ip_tunnels.h>
36
37 struct sk_buff;
38 struct sw_flow_mask;
39 struct flow_table;
40
41 struct sw_flow_actions {
42         struct rcu_head rcu;
43         u32 actions_len;
44         struct nlattr actions[];
45 };
46
47 /* Used to memset ovs_key_ipv4_tunnel padding. */
48 #define OVS_TUNNEL_KEY_SIZE                                     \
49         (offsetof(struct ovs_key_ipv4_tunnel, ipv4_ttl) +       \
50          FIELD_SIZEOF(struct ovs_key_ipv4_tunnel, ipv4_ttl))
51
52 struct ovs_key_ipv4_tunnel {
53         __be64 tun_id;
54         __be32 ipv4_src;
55         __be32 ipv4_dst;
56         __be16 tun_flags;
57         u8   ipv4_tos;
58         u8   ipv4_ttl;
59 };
60
61 static inline void ovs_flow_tun_key_init(struct ovs_key_ipv4_tunnel *tun_key,
62                                          const struct iphdr *iph, __be64 tun_id,
63                                          __be16 tun_flags)
64 {
65         tun_key->tun_id = tun_id;
66         tun_key->ipv4_src = iph->saddr;
67         tun_key->ipv4_dst = iph->daddr;
68         tun_key->ipv4_tos = iph->tos;
69         tun_key->ipv4_ttl = iph->ttl;
70         tun_key->tun_flags = tun_flags;
71
72         /* clear struct padding. */
73         memset((unsigned char *) tun_key + OVS_TUNNEL_KEY_SIZE, 0,
74                sizeof(*tun_key) - OVS_TUNNEL_KEY_SIZE);
75 }
76
77 struct sw_flow_key {
78         struct ovs_key_ipv4_tunnel tun_key;  /* Encapsulating tunnel key. */
79         struct {
80                 u32     priority;       /* Packet QoS priority. */
81                 u32     skb_mark;       /* SKB mark. */
82                 u16     in_port;        /* Input switch port (or DP_MAX_PORTS). */
83         } phy;
84         struct {
85                 u8     src[ETH_ALEN];   /* Ethernet source address. */
86                 u8     dst[ETH_ALEN];   /* Ethernet destination address. */
87                 __be16 tci;             /* 0 if no VLAN, VLAN_TAG_PRESENT set otherwise. */
88                 __be16 type;            /* Ethernet frame type. */
89         } eth;
90         struct {
91                 u8     proto;           /* IP protocol or lower 8 bits of ARP opcode. */
92                 u8     tos;             /* IP ToS. */
93                 u8     ttl;             /* IP TTL/hop limit. */
94                 u8     frag;            /* One of OVS_FRAG_TYPE_*. */
95         } ip;
96         union {
97                 struct {
98                         struct {
99                                 __be32 src;     /* IP source address. */
100                                 __be32 dst;     /* IP destination address. */
101                         } addr;
102                         union {
103                                 struct {
104                                         __be16 src;             /* TCP/UDP/SCTP source port. */
105                                         __be16 dst;             /* TCP/UDP/SCTP destination port. */
106                                 } tp;
107                                 struct {
108                                         u8 sha[ETH_ALEN];       /* ARP source hardware address. */
109                                         u8 tha[ETH_ALEN];       /* ARP target hardware address. */
110                                 } arp;
111                         };
112                 } ipv4;
113                 struct {
114                         struct {
115                                 struct in6_addr src;    /* IPv6 source address. */
116                                 struct in6_addr dst;    /* IPv6 destination address. */
117                         } addr;
118                         __be32 label;                   /* IPv6 flow label. */
119                         struct {
120                                 __be16 src;             /* TCP/UDP/SCTP source port. */
121                                 __be16 dst;             /* TCP/UDP/SCTP destination port. */
122                         } tp;
123                         struct {
124                                 struct in6_addr target; /* ND target address. */
125                                 u8 sll[ETH_ALEN];       /* ND source link layer address. */
126                                 u8 tll[ETH_ALEN];       /* ND target link layer address. */
127                         } nd;
128                 } ipv6;
129         };
130 } __aligned(__alignof__(long));
131
132 struct sw_flow {
133         struct rcu_head rcu;
134         struct hlist_node hash_node[2];
135         u32 hash;
136
137         struct sw_flow_key key;
138         struct sw_flow_key unmasked_key;
139         struct sw_flow_mask *mask;
140         struct sw_flow_actions __rcu *sf_acts;
141
142         spinlock_t lock;        /* Lock for values below. */
143         unsigned long used;     /* Last used time (in jiffies). */
144         u64 packet_count;       /* Number of packets matched. */
145         u64 byte_count;         /* Number of bytes matched. */
146         u8 tcp_flags;           /* Union of seen TCP flags. */
147 };
148
149 struct sw_flow_key_range {
150         size_t start;
151         size_t end;
152 };
153
154 struct sw_flow_match {
155         struct sw_flow_key *key;
156         struct sw_flow_key_range range;
157         struct sw_flow_mask *mask;
158 };
159
160 void ovs_match_init(struct sw_flow_match *match,
161                 struct sw_flow_key *key, struct sw_flow_mask *mask);
162
163 struct arp_eth_header {
164         __be16      ar_hrd;     /* format of hardware address   */
165         __be16      ar_pro;     /* format of protocol address   */
166         unsigned char   ar_hln; /* length of hardware address   */
167         unsigned char   ar_pln; /* length of protocol address   */
168         __be16      ar_op;      /* ARP opcode (command)     */
169
170         /* Ethernet+IPv4 specific members. */
171         unsigned char       ar_sha[ETH_ALEN];   /* sender hardware address  */
172         unsigned char       ar_sip[4];          /* sender IP address        */
173         unsigned char       ar_tha[ETH_ALEN];   /* target hardware address  */
174         unsigned char       ar_tip[4];          /* target IP address        */
175 } __packed;
176
177 int ovs_flow_init(void);
178 void ovs_flow_exit(void);
179
180 struct sw_flow *ovs_flow_alloc(void);
181 void ovs_flow_deferred_free(struct sw_flow *);
182 void ovs_flow_free(struct sw_flow *, bool deferred);
183
184 struct sw_flow_actions *ovs_flow_actions_alloc(int actions_len);
185 void ovs_flow_deferred_free_acts(struct sw_flow_actions *);
186
187 int ovs_flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *);
188 void ovs_flow_used(struct sw_flow *, struct sk_buff *);
189 u64 ovs_flow_used_time(unsigned long flow_jiffies);
190 int ovs_flow_to_nlattrs(const struct sw_flow_key *,
191                 const struct sw_flow_key *, struct sk_buff *);
192 int ovs_match_from_nlattrs(struct sw_flow_match *match,
193                       const struct nlattr *,
194                       const struct nlattr *);
195 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
196                 const struct nlattr *attr);
197
198 #define MAX_ACTIONS_BUFSIZE     (32 * 1024)
199 #define TBL_MIN_BUCKETS         1024
200
201 struct flow_table {
202         struct flex_array *buckets;
203         unsigned int count, n_buckets;
204         struct rcu_head rcu;
205         struct list_head *mask_list;
206         int node_ver;
207         u32 hash_seed;
208         bool keep_flows;
209 };
210
211 static inline int ovs_flow_tbl_count(struct flow_table *table)
212 {
213         return table->count;
214 }
215
216 static inline int ovs_flow_tbl_need_to_expand(struct flow_table *table)
217 {
218         return (table->count > table->n_buckets);
219 }
220
221 struct sw_flow *ovs_flow_lookup(struct flow_table *,
222                                 const struct sw_flow_key *);
223 struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
224                                     struct sw_flow_match *match);
225
226 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred);
227 struct flow_table *ovs_flow_tbl_alloc(int new_size);
228 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table);
229 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table);
230
231 void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow);
232 void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow);
233
234 struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *idx);
235 extern const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1];
236 int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
237                              struct sw_flow_match *match, bool is_mask);
238 int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
239                            const struct ovs_key_ipv4_tunnel *tun_key,
240                            const struct ovs_key_ipv4_tunnel *output);
241
242 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
243                 const struct sw_flow_key *key, int key_end);
244
245 struct sw_flow_mask {
246         int ref_count;
247         struct rcu_head rcu;
248         struct list_head list;
249         struct sw_flow_key_range range;
250         struct sw_flow_key key;
251 };
252
253 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void);
254 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *);
255 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *, bool deferred);
256 void ovs_sw_flow_mask_insert(struct flow_table *, struct sw_flow_mask *);
257 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *,
258                 const struct sw_flow_mask *);
259 void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
260                        const struct sw_flow_mask *mask);
261 #endif /* flow.h */