Don't need 'deleted' member of struct flow.
[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/rcupdate.h>
9 #include <linux/gfp.h>
10 #include <linux/skbuff.h>
11
12 #include "openflow.h"
13
14 struct sk_buff;
15 struct ofp_flow_mod;
16
17 /* Identification data for a flow.
18    Network byte order except for the "wildcards" field.
19    In decreasing order by size, so that sw_flow_key structures can
20    be hashed or compared bytewise.
21    It might be useful to reorder members from (expected) greatest to least
22    inter-flow variability, so that failing bytewise comparisons with memcmp
23    terminate as quickly as possible on average. */
24 struct sw_flow_key {
25         uint32_t nw_src;                /* IP source address. */
26         uint32_t nw_dst;                /* IP destination address. */
27         uint16_t in_port;           /* Input switch port */
28         uint16_t dl_vlan;           /* Input VLAN. */
29         uint16_t dl_type;           /* Ethernet frame type. */
30         uint16_t tp_src;        /* TCP/UDP source port. */
31         uint16_t tp_dst;        /* TCP/UDP destination port. */
32         uint16_t wildcards;         /* Wildcard fields (host byte order). */
33         uint8_t dl_src[6];          /* Ethernet source address. */
34         uint8_t dl_dst[6];          /* Ethernet destination address. */
35         uint8_t nw_proto;               /* IP protocol. */
36         uint8_t pad[3];             /* NB: Pad to make 32-bit aligned */
37 };
38
39 /* We need to manually make sure that the structure is 32-bit aligned,
40  * since we don't want garbage values in compiler-generated pads from
41  * messing up hash matches.
42  */
43 static inline void check_key_align(void)
44 {
45         BUILD_BUG_ON(sizeof(struct sw_flow_key) != 36); 
46 }
47
48 /* Locking:
49  *
50  * - Readers must take rcu_read_lock and hold it the entire time that the flow
51  *   must continue to exist.
52  *
53  * - Writers must hold dp_mutex.
54  */
55 struct sw_flow {
56         struct sw_flow_key key;
57
58         uint16_t max_idle;      /* Idle time before discarding (seconds). */
59         uint16_t priority;      /* Only used on entries with wildcards. */
60         unsigned long timeout;  /* Expiration time (in jiffies). */
61
62         /* FIXME?  Probably most flows have only a single action. */
63         unsigned int n_actions;
64         struct ofp_action *actions;
65
66         /* For use by table implementation. */
67         struct list_head node;
68         struct list_head iter_node;
69         unsigned long serial;
70         void *private;
71
72         spinlock_t lock;         /* Lock this entry...mostly for stat updates */
73         unsigned long init_time; /* When the flow was created (in jiffies). */
74         uint64_t packet_count;   /* Number of packets associated with this entry */
75         uint64_t byte_count;     /* Number of bytes associated with this entry */
76
77         struct rcu_head rcu;
78 };
79
80 int flow_matches(const struct sw_flow_key *, const struct sw_flow_key *);
81 int flow_del_matches(const struct sw_flow_key *, const struct sw_flow_key *, 
82                 int);
83 struct sw_flow *flow_alloc(int n_actions, gfp_t flags);
84 void flow_free(struct sw_flow *);
85 void flow_deferred_free(struct sw_flow *);
86 void flow_extract(struct sk_buff *, uint16_t in_port, struct sw_flow_key *);
87 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from);
88 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from);
89
90 void print_flow(const struct sw_flow_key *);
91
92 #include <linux/jiffies.h>
93 static inline int flow_timeout(struct sw_flow *flow)
94 {
95         if (flow->max_idle == OFP_FLOW_PERMANENT)
96                 return 0;
97
98         return time_after(jiffies, flow->timeout);
99 }
100
101 static inline void flow_used(struct sw_flow *flow, struct sk_buff *skb) 
102 {
103         unsigned long flags;
104
105         if (flow->max_idle != OFP_FLOW_PERMANENT)
106                 flow->timeout = jiffies + HZ * flow->max_idle;
107
108         spin_lock_irqsave(&flow->lock, flags);
109         flow->packet_count++;
110         flow->byte_count += skb->len;
111         spin_unlock_irqrestore(&flow->lock, flags);
112 }
113
114 extern struct kmem_cache *flow_cache;
115
116 int flow_init(void);
117 void flow_exit(void);
118
119 #endif /* flow.h */