fde6dd37d9cc2e3f4831c438355b29047bbe0690
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
4  * Stanford Junior University
5  */
6
7 #include "flow.h"
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_vlan.h>
12 #include <net/llc_pdu.h>
13 #include <linux/ip.h>
14 #include <linux/kernel.h>
15 #include <linux/tcp.h>
16 #include <linux/udp.h>
17 #include <linux/in.h>
18 #include <linux/rcupdate.h>
19
20 #include "openflow.h"
21 #include "compat.h"
22 #include "snap.h"
23
24 struct kmem_cache *flow_cache;
25
26 /* Internal function used to compare fields in flow. */
27 static inline
28 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
29                 uint16_t w)
30 {
31         return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
32                 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
33                 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
34                 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
35                 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
36                 && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
37                 && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
38                 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
39                 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
40                 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
41 }
42
43 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
44  * modulo wildcards, zero otherwise. */
45 inline
46 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
47 {
48         return flow_fields_match(a, b, (a->wildcards | b->wildcards));
49 }
50
51 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key 
52  * describing the deletion) match, that is, if their fields are 
53  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
54  * wildcards must match in both 't_key' and 'd_key'.  Note that the
55  * table's wildcards are ignored unless 'strict' is set. */
56 inline
57 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
58 {
59         if (strict && (t->wildcards != d->wildcards))
60                 return 0;
61
62         return flow_fields_match(t, d, d->wildcards);
63 }
64
65 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
66 {
67         to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
68         memset(to->pad, '\0', sizeof(to->pad));
69         to->in_port = from->in_port;
70         to->dl_vlan = from->dl_vlan;
71         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
72         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
73         to->dl_type = from->dl_type;
74
75         if (likely(from->dl_type == htons(ETH_P_IP))) {
76                 to->nw_src   = from->nw_src;
77                 to->nw_dst   = from->nw_dst;
78                 to->nw_proto = from->nw_proto;
79
80                 if ((from->nw_proto != IPPROTO_TCP && from->nw_proto != IPPROTO_UDP)) {
81                         goto no_th;
82                 }
83                 to->tp_src = from->tp_src;
84                 to->tp_dst = from->tp_dst;
85                 return;
86         }
87
88         to->nw_src = 0;
89         to->nw_dst = 0;
90         to->nw_proto = 0;
91
92 no_th:
93         to->tp_src = 0;
94         to->tp_dst = 0;
95 }
96
97 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
98 {
99         to->wildcards = htons(from->wildcards);
100         to->in_port   = from->in_port;
101         to->dl_vlan   = from->dl_vlan;
102         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
103         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
104         to->dl_type   = from->dl_type;
105         to->nw_src        = from->nw_src;
106         to->nw_dst        = from->nw_dst;
107         to->nw_proto  = from->nw_proto;
108         to->tp_src        = from->tp_src;
109         to->tp_dst        = from->tp_dst;
110         memset(to->pad, '\0', sizeof(to->pad));
111 }
112
113 /* Returns true if 'flow' can be deleted and set up for a deferred free, false
114  * if deletion has already been scheduled (by another thread).
115  *
116  * Caller must hold rcu_read_lock. */
117 int flow_del(struct sw_flow *flow)
118 {
119         return !atomic_cmpxchg(&flow->deleted, 0, 1);
120 }
121
122 /* Allocates and returns a new flow with 'n_actions' action, using allocation
123  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
124 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
125 {
126         struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
127         if (unlikely(!flow))
128                 return NULL;
129
130         flow->n_actions = n_actions;
131         flow->actions = kmalloc(n_actions * sizeof *flow->actions,
132                                 flags);
133         if (unlikely(!flow->actions) && n_actions > 0) {
134                 kmem_cache_free(flow_cache, flow);
135                 return NULL;
136         }
137         return flow;
138 }
139
140 /* Frees 'flow' immediately. */
141 void flow_free(struct sw_flow *flow)
142 {
143         if (unlikely(!flow))
144                 return;
145         kfree(flow->actions);
146         kmem_cache_free(flow_cache, flow);
147 }
148
149 /* RCU callback used by flow_deferred_free. */
150 static void rcu_callback(struct rcu_head *rcu)
151 {
152         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
153         flow_free(flow);
154 }
155
156 /* Schedules 'flow' to be freed after the next RCU grace period.
157  * The caller must hold rcu_read_lock for this to be sensible. */
158 void flow_deferred_free(struct sw_flow *flow)
159 {
160         call_rcu(&flow->rcu, rcu_callback);
161 }
162
163 /* Prints a representation of 'key' to the kernel log. */
164 void print_flow(const struct sw_flow_key *key)
165 {
166         printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
167                         "->%02x:%02x:%02x:%02x:%02x:%02x "
168                         "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
169                         key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
170                         key->dl_src[0], key->dl_src[1], key->dl_src[2],
171                         key->dl_src[3], key->dl_src[4], key->dl_src[5],
172                         key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
173                         key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
174                         ntohs(key->dl_type),
175                         ((unsigned char *)&key->nw_src)[0],
176                         ((unsigned char *)&key->nw_src)[1],
177                         ((unsigned char *)&key->nw_src)[2],
178                         ((unsigned char *)&key->nw_src)[3],
179                         ((unsigned char *)&key->nw_dst)[0],
180                         ((unsigned char *)&key->nw_dst)[1],
181                         ((unsigned char *)&key->nw_dst)[2],
182                         ((unsigned char *)&key->nw_dst)[3],
183                         ntohs(key->tp_src), ntohs(key->tp_dst));
184 }
185
186 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
187  * and initializes 'key' to match. */
188 void flow_extract(struct sk_buff *skb, uint16_t in_port,
189                   struct sw_flow_key *key)
190 {
191         struct ethhdr *mac;
192         struct udphdr *th;
193         int nh_ofs, th_ofs;
194
195         key->in_port = htons(in_port);
196         key->wildcards = 0;
197         memset(key->pad, '\0', sizeof(key->pad));
198
199         /* This code doesn't check that skb->len is long enough to contain the
200          * MAC or network header.  With a 46-byte minimum length frame this
201          * assumption is always correct. */
202
203         /* Doesn't verify checksums.  Should it? */
204
205         /* Data link layer.  We only support Ethernet. */
206         mac = eth_hdr(skb);
207         nh_ofs = sizeof(struct ethhdr);
208         if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
209                 /* This is an Ethernet II frame */
210                 key->dl_type = mac->h_proto;
211         } else {
212                 /* This is an 802.2 frame */
213                 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
214                         nh_ofs += sizeof(struct snap_hdr);
215                 } else {
216                         key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
217                         nh_ofs += sizeof(struct llc_pdu_un);
218                 }
219         }
220
221         /* Check for a VLAN tag */
222         if (likely(key->dl_type != htons(ETH_P_8021Q))) {
223                 key->dl_vlan = htons(OFP_VLAN_NONE);
224         } else {
225                 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
226                 key->dl_type = vh->h_vlan_encapsulated_proto;
227                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
228                 nh_ofs += sizeof(*vh);
229         }
230         memcpy(key->dl_src, mac->h_source, ETH_ALEN);
231         memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
232         skb_set_network_header(skb, nh_ofs);
233
234         /* Network layer. */
235         if (likely(key->dl_type == htons(ETH_P_IP))) {
236                 struct iphdr *nh = ip_hdr(skb);
237                 key->nw_src = nh->saddr;
238                 key->nw_dst = nh->daddr;
239                 key->nw_proto = nh->protocol;
240                 th_ofs = nh_ofs + nh->ihl * 4;
241                 skb_set_transport_header(skb, th_ofs);
242
243                 /* Transport layer. */
244                 if ((key->nw_proto != IPPROTO_TCP && key->nw_proto != IPPROTO_UDP)
245                                 || skb->len < th_ofs + sizeof(struct udphdr)) {
246                         goto no_th;
247                 }
248                 th = udp_hdr(skb);
249                 key->tp_src = th->source;
250                 key->tp_dst = th->dest;
251
252                 return;
253         }
254
255         key->nw_src = 0;
256         key->nw_dst = 0;
257         key->nw_proto = 0;
258
259 no_th:
260         key->tp_src = 0;
261         key->tp_dst = 0;
262 }
263
264 /* Initializes the flow module.
265  * Returns zero if successful or a negative error code. */
266 int flow_init(void)
267 {
268         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
269                                         0, NULL);
270         if (flow_cache == NULL)
271                 return -ENOMEM;
272
273         return 0;
274 }
275
276 /* Uninitializes the flow module. */
277 void flow_exit(void)
278 {
279         kmem_cache_destroy(flow_cache);
280 }
281