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