Add support for listing and deleting entries based on an output port.
[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/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  * Ordered to make bytewise comparisons (e.g. with memcmp()) fail quickly and
21  * to keep the amount of padding to a minimum.
22  * If you change the ordering of fields here, change flow_keys_equal() to
23  * compare the proper fields.
24  */
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         uint8_t dl_src[ETH_ALEN]; /* Ethernet source address. */
34         uint8_t dl_dst[ETH_ALEN]; /* Ethernet destination address. */
35         uint8_t nw_proto;       /* IP protocol. */
36         uint8_t pad;            /* Pad to 32-bit alignment. */
37         uint32_t wildcards;     /* Wildcard fields (host byte order). */
38         uint32_t nw_src_mask;   /* 1-bit in each significant nw_src bit. */
39         uint32_t nw_dst_mask;   /* 1-bit in each significant nw_dst bit. */
40 };
41
42 /* The match fields for ICMP type and code use the transport source and 
43  * destination port fields, respectively. */
44 #define icmp_type tp_src
45 #define icmp_code tp_dst
46
47 /* Compare two sw_flow_keys and return true if they are the same flow, false
48  * otherwise.  Wildcards and netmasks are not considered. */
49 static inline int flow_keys_equal(const struct sw_flow_key *a,
50                                    const struct sw_flow_key *b) 
51 {
52         return !memcmp(a, b, offsetof(struct sw_flow_key, wildcards));
53 }
54
55 /* We need to manually make sure that the structure is 32-bit aligned,
56  * since we don't want garbage values in compiler-generated pads from
57  * messing up hash matches.
58  */
59 static inline void check_key_align(void)
60 {
61         BUILD_BUG_ON(sizeof(struct sw_flow_key) != 44); 
62 }
63
64 /* We keep actions as a separate structure because we need to be able to 
65  * swap them out atomically when the modify command comes from a Flow
66  * Modify message. */
67 struct sw_flow_actions {
68         size_t actions_len;
69         struct rcu_head rcu;
70
71         struct ofp_action_header actions[0];
72 };
73
74 /* Locking:
75  *
76  * - Readers must take rcu_read_lock and hold it the entire time that the flow
77  *   must continue to exist.
78  *
79  * - Writers must hold dp_mutex.
80  */
81 struct sw_flow {
82         struct sw_flow_key key;
83
84         uint16_t priority;      /* Only used on entries with wildcards. */
85         uint16_t idle_timeout;  /* Idle time before discarding (seconds). */
86         uint16_t hard_timeout;  /* Hard expiration time (seconds) */
87         unsigned long used;     /* Last used time (in jiffies). */
88
89         struct sw_flow_actions *sf_acts;
90
91         /* For use by table implementation. */
92         struct list_head node;
93         struct list_head iter_node;
94         unsigned long serial;
95         void *private;
96
97         spinlock_t lock;         /* Lock this entry...mostly for stat updates */
98         unsigned long init_time; /* When the flow was created (in jiffies). */
99         uint64_t packet_count;   /* Number of packets associated with this entry */
100         uint64_t byte_count;     /* Number of bytes associated with this entry */
101
102         struct rcu_head rcu;
103 };
104
105 int flow_matches_1wild(const struct sw_flow_key *, const struct sw_flow_key *);
106 int flow_matches_2wild(const struct sw_flow_key *, const struct sw_flow_key *);
107 int flow_matches_desc(const struct sw_flow_key *, const struct sw_flow_key *, 
108                 int);
109 int flow_has_out_port(struct sw_flow *, uint16_t);
110 struct sw_flow *flow_alloc(size_t actions_len, gfp_t flags);
111 void flow_free(struct sw_flow *);
112 void flow_deferred_free(struct sw_flow *);
113 void flow_deferred_free_acts(struct sw_flow_actions *);
114 void flow_replace_acts(struct sw_flow *, const struct ofp_action_header *, 
115                 size_t);
116 int flow_extract(struct sk_buff *, uint16_t in_port, struct sw_flow_key *);
117 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from);
118 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from);
119 int flow_timeout(struct sw_flow *);
120
121 void print_flow(const struct sw_flow_key *);
122
123 static inline void flow_used(struct sw_flow *flow, struct sk_buff *skb) 
124 {
125         unsigned long flags;
126
127         flow->used = jiffies;
128
129         spin_lock_irqsave(&flow->lock, flags);
130         flow->packet_count++;
131         flow->byte_count += skb->len;
132         spin_unlock_irqrestore(&flow->lock, flags);
133 }
134
135 extern struct kmem_cache *flow_cache;
136
137 int flow_init(void);
138 void flow_exit(void);
139
140 #endif /* flow.h */