datapath: Fix coding style issues.
[sliver-openvswitch.git] / datapath / flow.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 #ifndef FLOW_H
10 #define FLOW_H 1
11
12 #include <linux/kernel.h>
13 #include <linux/netlink.h>
14 #include <linux/openvswitch.h>
15 #include <linux/spinlock.h>
16 #include <linux/types.h>
17 #include <linux/rcupdate.h>
18 #include <linux/if_ether.h>
19 #include <linux/in6.h>
20 #include <linux/jiffies.h>
21 #include <linux/time.h>
22 #include <linux/flex_array.h>
23 #include <net/inet_ecn.h>
24
25 struct sk_buff;
26
27 struct sw_flow_actions {
28         struct rcu_head rcu;
29         u32 actions_len;
30         struct nlattr actions[];
31 };
32
33 /* Mask for the OVS_FRAG_TYPE_* value in the low 2 bits of ip.tos_frag in
34  * struct sw_flow_key. */
35 #define OVS_FRAG_TYPE_MASK INET_ECN_MASK
36
37 struct sw_flow_key {
38         struct {
39                 __be64  tun_id;         /* Encapsulating tunnel ID. */
40                 u32     priority;       /* Packet QoS priority. */
41                 u16     in_port;        /* Input switch port (or USHRT_MAX). */
42         } phy;
43         struct {
44                 u8     src[ETH_ALEN];   /* Ethernet source address. */
45                 u8     dst[ETH_ALEN];   /* Ethernet destination address. */
46                 __be16 tci;             /* 0 if no VLAN, VLAN_TAG_PRESENT set otherwise. */
47                 __be16 type;            /* Ethernet frame type. */
48         } eth;
49         struct {
50                 u8     proto;           /* IP protocol or lower 8 bits of ARP opcode. */
51                 u8     tos_frag;        /* IP ToS DSCP in high 6 bits,
52                                          * OVS_FRAG_TYPE_* in low 2 bits. */
53         } ip;
54         union {
55                 struct {
56                         struct {
57                                 __be32 src;     /* IP source address. */
58                                 __be32 dst;     /* IP destination address. */
59                         } addr;
60                         union {
61                                 struct {
62                                         __be16 src;             /* TCP/UDP source port. */
63                                         __be16 dst;             /* TCP/UDP destination port. */
64                                 } tp;
65                                 struct {
66                                         u8 sha[ETH_ALEN];       /* ARP source hardware address. */
67                                         u8 tha[ETH_ALEN];       /* ARP target hardware address. */
68                                 } arp;
69                         };
70                 } ipv4;
71                 struct {
72                         struct {
73                                 struct in6_addr src;    /* IPv6 source address. */
74                                 struct in6_addr dst;    /* IPv6 destination address. */
75                         } addr;
76                         struct {
77                                 __be16 src;             /* TCP/UDP source port. */
78                                 __be16 dst;             /* TCP/UDP destination port. */
79                         } tp;
80                         struct {
81                                 struct in6_addr target; /* ND target address. */
82                                 u8 sll[ETH_ALEN];       /* ND source link layer address. */
83                                 u8 tll[ETH_ALEN];       /* ND target link layer address. */
84                         } nd;
85                 } ipv6;
86         };
87 };
88
89 struct sw_flow {
90         struct rcu_head rcu;
91         struct hlist_node  hash_node;
92         u32 hash;
93
94         struct sw_flow_key key;
95         struct sw_flow_actions __rcu *sf_acts;
96
97         atomic_t refcnt;
98         bool dead;
99
100         spinlock_t lock;        /* Lock for values below. */
101         unsigned long used;     /* Last used time (in jiffies). */
102         u64 packet_count;       /* Number of packets matched. */
103         u64 byte_count;         /* Number of bytes matched. */
104         u8 tcp_flags;           /* Union of seen TCP flags. */
105 };
106
107 struct arp_eth_header {
108         __be16      ar_hrd;     /* format of hardware address   */
109         __be16      ar_pro;     /* format of protocol address   */
110         unsigned char   ar_hln; /* length of hardware address   */
111         unsigned char   ar_pln; /* length of protocol address   */
112         __be16      ar_op;      /* ARP opcode (command)     */
113
114         /* Ethernet+IPv4 specific members. */
115         unsigned char       ar_sha[ETH_ALEN];   /* sender hardware address  */
116         unsigned char       ar_sip[4];          /* sender IP address        */
117         unsigned char       ar_tha[ETH_ALEN];   /* target hardware address  */
118         unsigned char       ar_tip[4];          /* target IP address        */
119 } __packed;
120
121 int flow_init(void);
122 void flow_exit(void);
123
124 struct sw_flow *flow_alloc(void);
125 void flow_deferred_free(struct sw_flow *);
126
127 struct sw_flow_actions *flow_actions_alloc(const struct nlattr *);
128 void flow_deferred_free_acts(struct sw_flow_actions *);
129
130 void flow_hold(struct sw_flow *);
131 void flow_put(struct sw_flow *);
132
133 int flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *,
134                  int *key_lenp);
135 void flow_used(struct sw_flow *, struct sk_buff *);
136 u64 flow_used_time(unsigned long flow_jiffies);
137
138 /* Upper bound on the length of a nlattr-formatted flow key.  The longest
139  * nlattr-formatted flow key would be:
140  *
141  *                         struct  pad  nl hdr  total
142  *                         ------  ---  ------  -----
143  *  OVS_KEY_ATTR_PRIORITY      4    --     4      8
144  *  OVS_KEY_ATTR_TUN_ID        8    --     4     12
145  *  OVS_KEY_ATTR_IN_PORT       4    --     4      8
146  *  OVS_KEY_ATTR_ETHERNET     12    --     4     16
147  *  OVS_KEY_ATTR_8021Q         4    --     4      8
148  *  OVS_KEY_ATTR_ETHERTYPE     2     2     4      8
149  *  OVS_KEY_ATTR_IPV6         34     2     4     40
150  *  OVS_KEY_ATTR_ICMPV6        2     2     4      8
151  *  OVS_KEY_ATTR_ND           28    --     4     32
152  *  -------------------------------------------------
153  *  total                                       140
154  */
155 #define FLOW_BUFSIZE 140
156
157 int flow_to_nlattrs(const struct sw_flow_key *, struct sk_buff *);
158 int flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
159                       const struct nlattr *);
160 int flow_metadata_from_nlattrs(u32 *priority, u16 *in_port, __be64 *tun_id,
161                                const struct nlattr *);
162
163 #define TBL_MIN_BUCKETS         1024
164
165 struct flow_table {
166         struct flex_array *buckets;
167         unsigned int count, n_buckets;
168         struct rcu_head rcu;
169 };
170
171 static inline int flow_tbl_count(struct flow_table *table)
172 {
173         return table->count;
174 }
175
176 static inline int flow_tbl_need_to_expand(struct flow_table *table)
177 {
178         return (table->count > table->n_buckets);
179 }
180
181 struct sw_flow *flow_tbl_lookup(struct flow_table *table,
182                                 struct sw_flow_key *key,    int len);
183 void flow_tbl_destroy(struct flow_table *table);
184 void flow_tbl_deferred_destroy(struct flow_table *table);
185 struct flow_table *flow_tbl_alloc(int new_size);
186 struct flow_table *flow_tbl_expand(struct flow_table *table);
187 void flow_tbl_insert(struct flow_table *table, struct sw_flow *flow);
188 void flow_tbl_remove(struct flow_table *table, struct sw_flow *flow);
189 u32 flow_hash(const struct sw_flow_key *key, int key_len);
190
191 struct sw_flow *flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *idx);
192 extern const u32 ovs_key_lens[OVS_KEY_ATTR_MAX + 1];
193
194 #endif /* flow.h */