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>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <linux/icmp.h>
19 #include "openflow/openflow.h"
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.
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. */
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
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)
58 return !memcmp(a, b, offsetof(struct sw_flow_key, wildcards));
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.
65 static inline void check_key_align(void)
67 BUILD_BUG_ON(sizeof(struct sw_flow_key) != 44);
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
73 struct sw_flow_actions {
77 struct ofp_action_header actions[0];
82 * - Readers must take rcu_read_lock and hold it the entire time that the flow
83 * must continue to exist.
85 * - Writers must hold dp_mutex.
88 struct sw_flow_key key;
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). */
95 struct sw_flow_actions *sf_acts;
97 /* For use by table implementation. */
98 struct list_head node;
99 struct list_head iter_node;
100 unsigned long serial;
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 */
108 uint8_t tcp_flags; /* Union of seen TCP flags. */
109 uint8_t ip_tos; /* IP TOS value. */
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 *,
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 *,
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 *);
130 void print_flow(const struct sw_flow_key *);
132 static inline int iphdr_ok(struct sk_buff *skb)
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));
143 static inline int tcphdr_ok(struct sk_buff *skb)
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);
154 static inline int udphdr_ok(struct sk_buff *skb)
156 int th_ofs = skb_transport_offset(skb);
157 return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
160 static inline int icmphdr_ok(struct sk_buff *skb)
162 int th_ofs = skb_transport_offset(skb);
163 return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
166 #define TCP_FLAGS_OFFSET 13
167 #define TCP_FLAG_MASK 0x3f
169 static inline struct ofp_tcphdr *ofp_tcp_hdr(const struct sk_buff *skb)
171 return (struct ofp_tcphdr *)skb_transport_header(skb);
174 static inline void flow_used(struct sw_flow *flow, struct sk_buff *skb)
178 flow->used = get_jiffies_64();
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;
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;
191 flow->packet_count++;
192 flow->byte_count += skb->len;
193 spin_unlock_irqrestore(&flow->lock, flags);
196 extern struct kmem_cache *flow_cache;
199 void flow_exit(void);