Implement subnet mask matching in OpenFlow.
[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 #include <linux/if_ether.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 wildcards;         /* Wildcard fields (host byte order). */
27         uint32_t nw_src;                /* IP source address. */
28         uint32_t nw_src_mask;   /* 1-bit in each significant nw_src bit. */
29         uint32_t nw_dst;                /* IP destination address. */
30         uint32_t nw_dst_mask;       /* 1-bit in each significant nw_dst bit. */
31         uint16_t in_port;           /* Input switch port */
32         uint16_t dl_vlan;           /* Input VLAN. */
33         uint16_t dl_type;           /* Ethernet frame type. */
34         uint16_t tp_src;        /* TCP/UDP source port. */
35         uint16_t tp_dst;        /* TCP/UDP destination port. */
36         uint8_t dl_src[ETH_ALEN];           /* Ethernet source address. */
37         uint8_t dl_dst[ETH_ALEN];           /* Ethernet destination address. */
38         uint8_t nw_proto;               /* IP protocol. */
39         uint8_t pad;                /* NB: Pad to make 32-bit aligned */
40 };
41
42 /* We need to manually make sure that the structure is 32-bit aligned,
43  * since we don't want garbage values in compiler-generated pads from
44  * messing up hash matches.
45  */
46 static inline void check_key_align(void)
47 {
48         BUILD_BUG_ON(sizeof(struct sw_flow_key) != 44); 
49 }
50
51 /* Locking:
52  *
53  * - Readers must take rcu_read_lock and hold it the entire time that the flow
54  *   must continue to exist.
55  *
56  * - Writers must hold dp_mutex.
57  */
58 struct sw_flow {
59         struct sw_flow_key key;
60
61         uint16_t priority;      /* Only used on entries with wildcards. */
62         uint16_t idle_timeout;  /* Idle time before discarding (seconds). */
63         uint16_t hard_timeout;  /* Hard expiration time (seconds) */
64         unsigned long used;     /* Last used time (in jiffies). */
65
66         /* FIXME?  Probably most flows have only a single action. */
67         unsigned int n_actions;
68         struct ofp_action *actions;
69
70         /* For use by table implementation. */
71         struct list_head node;
72         struct list_head iter_node;
73         unsigned long serial;
74         void *private;
75
76         spinlock_t lock;         /* Lock this entry...mostly for stat updates */
77         unsigned long init_time; /* When the flow was created (in jiffies). */
78         uint64_t packet_count;   /* Number of packets associated with this entry */
79         uint64_t byte_count;     /* Number of bytes associated with this entry */
80
81         struct rcu_head rcu;
82 };
83
84 int flow_matches_1wild(const struct sw_flow_key *, const struct sw_flow_key *);
85 int flow_matches_2wild(const struct sw_flow_key *, const struct sw_flow_key *);
86 int flow_del_matches(const struct sw_flow_key *, const struct sw_flow_key *, 
87                 int);
88 struct sw_flow *flow_alloc(int n_actions, gfp_t flags);
89 void flow_free(struct sw_flow *);
90 void flow_deferred_free(struct sw_flow *);
91 int flow_extract(struct sk_buff *, uint16_t in_port, struct sw_flow_key *);
92 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from);
93 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from);
94 int flow_timeout(struct sw_flow *);
95
96 void print_flow(const struct sw_flow_key *);
97
98 static inline void flow_used(struct sw_flow *flow, struct sk_buff *skb) 
99 {
100         unsigned long flags;
101
102         flow->used = jiffies;
103
104         spin_lock_irqsave(&flow->lock, flags);
105         flow->packet_count++;
106         flow->byte_count += skb->len;
107         spin_unlock_irqrestore(&flow->lock, flags);
108 }
109
110 extern struct kmem_cache *flow_cache;
111
112 int flow_init(void);
113 void flow_exit(void);
114
115 #endif /* flow.h */