Fix setting "of" device name based on unitialized dp_idx.
[sliver-openvswitch.git] / datapath / flow.h
1 #ifndef FLOW_H
2 #define FLOW_H 1
3
4 #include <linux/kernel.h>
5 #include <linux/spinlock.h>
6 #include <linux/list.h>
7 #include <linux/types.h>
8 #include <linux/jiffies.h>
9 #include <linux/rcupdate.h>
10 #include <linux/gfp.h>
11 #include <linux/skbuff.h>
12 #include <linux/if_ether.h>
13 #include <linux/ip.h>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <linux/icmp.h>
17 #include <net/ip.h>
18
19 #include "openflow/openflow.h"
20
21 struct sk_buff;
22 struct ofp_flow_mod;
23
24 /* Identification data for a flow.
25  * Network byte order except for the "wildcards" field.
26  * Ordered to make bytewise comparisons (e.g. with memcmp()) fail quickly and
27  * to keep the amount of padding to a minimum.
28  * If you change the ordering of fields here, change flow_keys_equal() to
29  * compare the proper fields.
30  */
31 struct sw_flow_key {
32         uint32_t nw_src;        /* IP source address. */
33         uint32_t nw_dst;        /* IP destination address. */
34         uint16_t in_port;       /* Input switch port */
35         uint16_t dl_vlan;       /* Input VLAN. */
36         uint16_t dl_type;       /* Ethernet frame type. */
37         uint16_t tp_src;        /* TCP/UDP source port. */
38         uint16_t tp_dst;        /* TCP/UDP destination port. */
39         uint8_t dl_src[ETH_ALEN]; /* Ethernet source address. */
40         uint8_t dl_dst[ETH_ALEN]; /* Ethernet destination address. */
41         uint8_t nw_proto;       /* IP protocol. */
42         uint8_t pad;            /* Pad to 32-bit alignment. */
43         uint32_t wildcards;     /* Wildcard fields (host byte order). */
44         uint32_t nw_src_mask;   /* 1-bit in each significant nw_src bit. */
45         uint32_t nw_dst_mask;   /* 1-bit in each significant nw_dst bit. */
46 };
47
48 /* The match fields for ICMP type and code use the transport source and 
49  * destination port fields, respectively. */
50 #define icmp_type tp_src
51 #define icmp_code tp_dst
52
53 /* Compare two sw_flow_keys and return true if they are the same flow, false
54  * otherwise.  Wildcards and netmasks are not considered. */
55 static inline int flow_keys_equal(const struct sw_flow_key *a,
56                                    const struct sw_flow_key *b) 
57 {
58         return !memcmp(a, b, offsetof(struct sw_flow_key, wildcards));
59 }
60
61 /* We need to manually make sure that the structure is 32-bit aligned,
62  * since we don't want garbage values in compiler-generated pads from
63  * messing up hash matches.
64  */
65 static inline void check_key_align(void)
66 {
67         BUILD_BUG_ON(sizeof(struct sw_flow_key) != 44); 
68 }
69
70 /* We keep actions as a separate structure because we need to be able to 
71  * swap them out atomically when the modify command comes from a Flow
72  * Modify message. */
73 struct sw_flow_actions {
74         size_t actions_len;
75         struct rcu_head rcu;
76
77         struct ofp_action_header actions[0];
78 };
79
80 /* Locking:
81  *
82  * - Readers must take rcu_read_lock and hold it the entire time that the flow
83  *   must continue to exist.
84  *
85  * - Writers must hold dp_mutex.
86  */
87 struct sw_flow {
88         struct sw_flow_key key;
89
90         uint16_t priority;      /* Only used on entries with wildcards. */
91         uint16_t idle_timeout;  /* Idle time before discarding (seconds). */
92         uint16_t hard_timeout;  /* Hard expiration time (seconds) */
93         uint64_t used;          /* Last used time (in jiffies). */
94
95         struct sw_flow_actions *sf_acts;
96
97         /* For use by table implementation. */
98         struct list_head node;
99         struct list_head iter_node;
100         unsigned long serial;
101         void *private;
102
103         spinlock_t lock;         /* Lock this entry...mostly for stat updates */
104         uint64_t created;        /* When the flow was created (in jiffies_64). */
105         uint64_t packet_count;   /* Number of packets associated with this entry */
106         uint64_t byte_count;     /* Number of bytes associated with this entry */
107
108         uint8_t tcp_flags;       /* Union of seen TCP flags. */
109         uint8_t ip_tos;          /* IP TOS value. */
110
111         struct rcu_head rcu;
112 };
113
114 int flow_matches_1wild(const struct sw_flow_key *, const struct sw_flow_key *);
115 int flow_matches_2wild(const struct sw_flow_key *, const struct sw_flow_key *);
116 int flow_matches_desc(const struct sw_flow_key *, const struct sw_flow_key *, 
117                 int);
118 int flow_has_out_port(struct sw_flow *, uint16_t);
119 struct sw_flow *flow_alloc(size_t actions_len, gfp_t flags);
120 void flow_free(struct sw_flow *);
121 void flow_deferred_free(struct sw_flow *);
122 void flow_deferred_free_acts(struct sw_flow_actions *);
123 void flow_replace_acts(struct sw_flow *, const struct ofp_action_header *, 
124                 size_t);
125 int flow_extract(struct sk_buff *, uint16_t in_port, struct sw_flow_key *);
126 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from);
127 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from);
128 int flow_timeout(struct sw_flow *);
129
130 void print_flow(const struct sw_flow_key *);
131
132 static inline int iphdr_ok(struct sk_buff *skb)
133 {
134         int nh_ofs = skb_network_offset(skb);
135         if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
136                 int ip_len = ip_hdrlen(skb);
137                 return (ip_len >= sizeof(struct iphdr)
138                         && pskb_may_pull(skb, nh_ofs + ip_len));
139         }
140         return 0;
141 }
142
143 static inline int tcphdr_ok(struct sk_buff *skb)
144 {
145         int th_ofs = skb_transport_offset(skb);
146         if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
147                 int tcp_len = tcp_hdrlen(skb);
148                 return (tcp_len >= sizeof(struct tcphdr)
149                         && skb->len >= th_ofs + tcp_len);
150         }
151         return 0;
152 }
153
154 static inline int udphdr_ok(struct sk_buff *skb)
155 {
156         int th_ofs = skb_transport_offset(skb);
157         return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
158 }
159
160 static inline int icmphdr_ok(struct sk_buff *skb)
161 {
162         int th_ofs = skb_transport_offset(skb);
163         return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
164 }
165
166 #define TCP_FLAGS_OFFSET 13
167 #define TCP_FLAG_MASK 0x3f
168
169 static inline struct ofp_tcphdr *ofp_tcp_hdr(const struct sk_buff *skb)
170 {
171         return (struct ofp_tcphdr *)skb_transport_header(skb);
172 }
173
174 static inline void flow_used(struct sw_flow *flow, struct sk_buff *skb) 
175 {
176         unsigned long flags;
177
178         flow->used = get_jiffies_64();
179
180         spin_lock_irqsave(&flow->lock, flags);
181         if (flow->key.dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
182                 struct iphdr *nh = ip_hdr(skb);
183                 flow->ip_tos = nh->tos;
184
185                 if (flow->key.nw_proto == IPPROTO_TCP && tcphdr_ok(skb)) {
186                         uint8_t *tcp = (uint8_t *)tcp_hdr(skb);
187                         flow->tcp_flags |= *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
188                 }
189         }
190
191         flow->packet_count++;
192         flow->byte_count += skb->len;
193         spin_unlock_irqrestore(&flow->lock, flags);
194 }
195
196 extern struct kmem_cache *flow_cache;
197
198 int flow_init(void);
199 void flow_exit(void);
200
201 #endif /* flow.h */