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