Make datapath's flow_extract() properly pull data into the headers.
[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/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/llc.h>
17 #include <linux/module.h>
18 #include <linux/tcp.h>
19 #include <linux/udp.h>
20 #include <linux/in.h>
21 #include <linux/rcupdate.h>
22 #include <net/ip.h>
23
24 #include "openflow/openflow.h"
25 #include "compat.h"
26
27 struct kmem_cache *flow_cache;
28
29 /* Internal function used to compare fields in flow. */
30 static inline
31 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
32                       uint32_t w, uint32_t src_mask, uint32_t dst_mask)
33 {
34         return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
35                 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
36                 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
37                 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
38                 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
39                 && !((a->nw_src ^ b->nw_src) & src_mask)
40                 && !((a->nw_dst ^ b->nw_dst) & dst_mask)
41                 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
42                 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
43                 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
44 }
45
46 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
47  * modulo wildcards in 'b', zero otherwise. */
48 int flow_matches_1wild(const struct sw_flow_key *a,
49                        const struct sw_flow_key *b)
50 {
51         return flow_fields_match(a, b, b->wildcards,
52                                  b->nw_src_mask, b->nw_dst_mask);
53 }
54 EXPORT_SYMBOL(flow_matches_1wild);
55
56 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
57  * modulo wildcards in 'a' or 'b', zero otherwise. */
58 int flow_matches_2wild(const struct sw_flow_key *a,
59                        const struct sw_flow_key *b)
60 {
61         return flow_fields_match(a, b,
62                                  a->wildcards | b->wildcards,
63                                  a->nw_src_mask & b->nw_src_mask,
64                                  a->nw_dst_mask & b->nw_dst_mask);
65 }
66 EXPORT_SYMBOL(flow_matches_2wild);
67
68 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key
69  * describing the match) match, that is, if their fields are
70  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
71  * wildcards must match in both 't_key' and 'd_key'.  Note that the
72  * table's wildcards are ignored unless 'strict' is set. */
73 int flow_matches_desc(const struct sw_flow_key *t, const struct sw_flow_key *d, 
74                 int strict)
75 {
76         if (strict && d->wildcards != t->wildcards)
77                 return 0;
78         return flow_matches_1wild(t, d);
79 }
80 EXPORT_SYMBOL(flow_matches_desc);
81
82 static uint32_t make_nw_mask(int n_wild_bits)
83 {
84         n_wild_bits &= (1u << OFPFW_NW_SRC_BITS) - 1;
85         return n_wild_bits < 32 ? htonl(~((1u << n_wild_bits) - 1)) : 0;
86 }
87
88 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
89 {
90         to->wildcards = ntohl(from->wildcards) & OFPFW_ALL;
91         to->pad = 0;
92         to->in_port = from->in_port;
93         to->dl_vlan = from->dl_vlan;
94         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
95         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
96         to->dl_type = from->dl_type;
97
98         to->nw_src = to->nw_dst = to->nw_proto = 0;
99         to->tp_src = to->tp_dst = 0;
100
101 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
102 #define OFPFW_NW (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO)
103         if (to->wildcards & OFPFW_DL_TYPE) {
104                 /* Can't sensibly match on network or transport headers if the
105                  * data link type is unknown. */
106                 to->wildcards |= OFPFW_NW | OFPFW_TP;
107         } else if (from->dl_type == htons(ETH_P_IP)) {
108                 to->nw_src   = from->nw_src;
109                 to->nw_dst   = from->nw_dst;
110                 to->nw_proto = from->nw_proto;
111
112                 if (to->wildcards & OFPFW_NW_PROTO) {
113                         /* Can't sensibly match on transport headers if the
114                          * network protocol is unknown. */
115                         to->wildcards |= OFPFW_TP;
116                 } else if (from->nw_proto == IPPROTO_TCP
117                            || from->nw_proto == IPPROTO_UDP) {
118                         to->tp_src = from->tp_src;
119                         to->tp_dst = from->tp_dst;
120                 } else {
121                         /* Transport layer fields are undefined.  Mark them as
122                          * exact-match to allow such flows to reside in
123                          * table-hash, instead of falling into table-linear. */
124                         to->wildcards &= ~OFPFW_TP;
125                 }
126         } else {
127                 /* Network and transport layer fields are undefined.  Mark them
128                  * as exact-match to allow such flows to reside in table-hash,
129                  * instead of falling into table-linear. */
130                 to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
131         }
132
133         /* We set these late because code above adjusts to->wildcards. */
134         to->nw_src_mask = make_nw_mask(to->wildcards >> OFPFW_NW_SRC_SHIFT);
135         to->nw_dst_mask = make_nw_mask(to->wildcards >> OFPFW_NW_DST_SHIFT);
136 }
137
138 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
139 {
140         to->wildcards = htonl(from->wildcards);
141         to->in_port   = from->in_port;
142         to->dl_vlan   = from->dl_vlan;
143         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
144         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
145         to->dl_type   = from->dl_type;
146         to->nw_src    = from->nw_src;
147         to->nw_dst    = from->nw_dst;
148         to->nw_proto  = from->nw_proto;
149         to->tp_src    = from->tp_src;
150         to->tp_dst    = from->tp_dst;
151         to->pad       = 0;
152 }
153
154 int flow_timeout(struct sw_flow *flow)
155 {
156         if (flow->idle_timeout != OFP_FLOW_PERMANENT
157             && time_after(jiffies, flow->used + flow->idle_timeout * HZ))
158                 return OFPER_IDLE_TIMEOUT;
159         else if (flow->hard_timeout != OFP_FLOW_PERMANENT
160                  && time_after(jiffies,
161                                flow->init_time + flow->hard_timeout * HZ))
162                 return OFPER_HARD_TIMEOUT;
163         else
164                 return -1;
165 }
166 EXPORT_SYMBOL(flow_timeout);
167
168 /* Allocates and returns a new flow with room for 'actions_len' actions, 
169  * using allocation flags 'flags'.  Returns the new flow or a null pointer 
170  * on failure. */
171 struct sw_flow *flow_alloc(size_t actions_len, gfp_t flags)
172 {
173         struct sw_flow_actions *sfa;
174         size_t size = sizeof *sfa + actions_len;
175         struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
176         if (unlikely(!flow))
177                 return NULL;
178
179         sfa = kmalloc(size, flags);
180         if (unlikely(!sfa)) {
181                 kmem_cache_free(flow_cache, flow);
182                 return NULL;
183         }
184         sfa->actions_len = actions_len;
185         flow->sf_acts = sfa;
186
187         return flow;
188 }
189
190 /* Frees 'flow' immediately. */
191 void flow_free(struct sw_flow *flow)
192 {
193         if (unlikely(!flow))
194                 return;
195         kfree(flow->sf_acts);
196         kmem_cache_free(flow_cache, flow);
197 }
198 EXPORT_SYMBOL(flow_free);
199
200 /* RCU callback used by flow_deferred_free. */
201 static void rcu_free_flow_callback(struct rcu_head *rcu)
202 {
203         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
204         flow_free(flow);
205 }
206
207 /* Schedules 'flow' to be freed after the next RCU grace period.
208  * The caller must hold rcu_read_lock for this to be sensible. */
209 void flow_deferred_free(struct sw_flow *flow)
210 {
211         call_rcu(&flow->rcu, rcu_free_flow_callback);
212 }
213 EXPORT_SYMBOL(flow_deferred_free);
214
215 /* RCU callback used by flow_deferred_free_acts. */
216 static void rcu_free_acts_callback(struct rcu_head *rcu)
217 {
218         struct sw_flow_actions *sf_acts = container_of(rcu, 
219                         struct sw_flow_actions, rcu);
220         kfree(sf_acts);
221 }
222
223 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
224  * The caller must hold rcu_read_lock for this to be sensible. */
225 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
226 {
227         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
228 }
229 EXPORT_SYMBOL(flow_deferred_free_acts);
230
231 /* Copies 'actions' into a newly allocated structure for use by 'flow'
232  * and safely frees the structure that defined the previous actions. */
233 void flow_replace_acts(struct sw_flow *flow, 
234                 const struct ofp_action_header *actions, size_t actions_len)
235 {
236         struct sw_flow_actions *sfa;
237         struct sw_flow_actions *orig_sfa = flow->sf_acts;
238         size_t size = sizeof *sfa + actions_len;
239
240         sfa = kmalloc(size, GFP_ATOMIC);
241         if (unlikely(!sfa))
242                 return;
243
244         sfa->actions_len = actions_len;
245         memcpy(sfa->actions, actions, actions_len);
246
247         rcu_assign_pointer(flow->sf_acts, sfa);
248         flow_deferred_free_acts(orig_sfa);
249
250         return;
251 }
252 EXPORT_SYMBOL(flow_replace_acts);
253
254 /* Prints a representation of 'key' to the kernel log. */
255 void print_flow(const struct sw_flow_key *key)
256 {
257         printk("wild%08x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
258                         "->%02x:%02x:%02x:%02x:%02x:%02x "
259                         "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
260                         key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
261                         key->dl_src[0], key->dl_src[1], key->dl_src[2],
262                         key->dl_src[3], key->dl_src[4], key->dl_src[5],
263                         key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
264                         key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
265                         ntohs(key->dl_type),
266                         ((unsigned char *)&key->nw_src)[0],
267                         ((unsigned char *)&key->nw_src)[1],
268                         ((unsigned char *)&key->nw_src)[2],
269                         ((unsigned char *)&key->nw_src)[3],
270                         ((unsigned char *)&key->nw_dst)[0],
271                         ((unsigned char *)&key->nw_dst)[1],
272                         ((unsigned char *)&key->nw_dst)[2],
273                         ((unsigned char *)&key->nw_dst)[3],
274                         ntohs(key->tp_src), ntohs(key->tp_dst));
275 }
276 EXPORT_SYMBOL(print_flow);
277
278 #define SNAP_OUI_LEN 3
279
280 struct eth_snap_hdr
281 {
282         struct ethhdr eth;
283         uint8_t  dsap;  /* Always 0xAA */
284         uint8_t  ssap;  /* Always 0xAA */
285         uint8_t  ctrl;
286         uint8_t  oui[SNAP_OUI_LEN];
287         uint16_t ethertype;
288 } __attribute__ ((packed));
289
290 static int is_snap(const struct eth_snap_hdr *esh)
291 {
292         return (esh->dsap == LLC_SAP_SNAP
293                 && esh->ssap == LLC_SAP_SNAP
294                 && !memcmp(esh->oui, "\0\0\0", 3));
295 }
296
297 static int iphdr_ok(struct sk_buff *skb)
298 {
299         int nh_ofs = skb_network_offset(skb);
300         if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
301                 int ip_len = ip_hdrlen(skb);
302                 return (ip_len >= sizeof(struct iphdr)
303                         && pskb_may_pull(skb, nh_ofs + ip_len));
304         }
305         return 0;
306 }
307
308 static int tcphdr_ok(struct sk_buff *skb)
309 {
310         int th_ofs = skb_transport_offset(skb);
311         if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
312                 int tcp_len = tcp_hdrlen(skb);
313                 return (tcp_len >= sizeof(struct tcphdr)
314                         && skb->len >= th_ofs + tcp_len);
315         }
316         return 0;
317 }
318
319 static int udphdr_ok(struct sk_buff *skb)
320 {
321         int th_ofs = skb_transport_offset(skb);
322         return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
323 }
324
325 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
326  * and initializes 'key' to match.  Returns 1 if 'skb' contains an IP
327  * fragment, 0 otherwise. */
328 int flow_extract(struct sk_buff *skb, uint16_t in_port,
329                  struct sw_flow_key *key)
330 {
331         struct ethhdr *eth;
332         struct eth_snap_hdr *esh;
333         int retval = 0;
334         int nh_ofs;
335
336         memset(key, 0, sizeof *key);
337         key->dl_vlan = htons(OFP_VLAN_NONE);
338         key->in_port = htons(in_port);
339
340         if (skb->len < sizeof *eth)
341                 return 0;
342         if (!pskb_may_pull(skb, skb->len >= 64 ? 64 : skb->len)) {
343                 return 0;
344         }
345
346         eth = eth_hdr(skb);
347         esh = (struct eth_snap_hdr *) eth;
348         nh_ofs = sizeof *eth;
349         if (likely(ntohs(eth->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF))
350                 key->dl_type = eth->h_proto;
351         else if (skb->len >= sizeof *esh && is_snap(esh)) {
352                 key->dl_type = esh->ethertype;
353                 nh_ofs = sizeof *esh;
354         } else {
355                 key->dl_type = htons(OFP_DL_TYPE_NOT_ETH_TYPE);
356                 if (skb->len >= nh_ofs + sizeof(struct llc_pdu_un)) {
357                         nh_ofs += sizeof(struct llc_pdu_un); 
358                 }
359         }
360
361         /* Check for a VLAN tag */
362         if (key->dl_type == htons(ETH_P_8021Q) &&
363             skb->len >= nh_ofs + sizeof(struct vlan_hdr)) {
364                 struct vlan_hdr *vh = (struct vlan_hdr*)(skb->data + nh_ofs);
365                 key->dl_type = vh->h_vlan_encapsulated_proto;
366                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
367                 nh_ofs += sizeof(struct vlan_hdr);
368         }
369         memcpy(key->dl_src, eth->h_source, ETH_ALEN);
370         memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
371         skb_set_network_header(skb, nh_ofs);
372
373         /* Network layer. */
374         if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
375                 struct iphdr *nh = ip_hdr(skb);
376                 int th_ofs = nh_ofs + nh->ihl * 4;
377                 key->nw_src = nh->saddr;
378                 key->nw_dst = nh->daddr;
379                 key->nw_proto = nh->protocol;
380                 skb_set_transport_header(skb, th_ofs);
381
382                 /* Transport layer. */
383                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
384                         if (key->nw_proto == IPPROTO_TCP) {
385                                 if (tcphdr_ok(skb)) {
386                                         struct tcphdr *tcp = tcp_hdr(skb);
387                                         key->tp_src = tcp->source;
388                                         key->tp_dst = tcp->dest;
389                                 } else {
390                                         /* Avoid tricking other code into
391                                          * thinking that this packet has an L4
392                                          * header. */
393                                         key->nw_proto = 0;
394                                 }
395                         } else if (key->nw_proto == IPPROTO_UDP) {
396                                 if (udphdr_ok(skb)) {
397                                         struct udphdr *udp = udp_hdr(skb);
398                                         key->tp_src = udp->source;
399                                         key->tp_dst = udp->dest;
400                                 } else {
401                                         /* Avoid tricking other code into
402                                          * thinking that this packet has an L4
403                                          * header. */
404                                         key->nw_proto = 0;
405                                 }
406                         }
407                 } else {
408                         retval = 1;
409                 }
410         } else {
411                 skb_reset_transport_header(skb);
412         }
413         return retval;
414 }
415
416 /* Initializes the flow module.
417  * Returns zero if successful or a negative error code. */
418 int flow_init(void)
419 {
420         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
421                                         0, NULL);
422         if (flow_cache == NULL)
423                 return -ENOMEM;
424
425         return 0;
426 }
427
428 /* Uninitializes the flow module. */
429 void flow_exit(void)
430 {
431         kmem_cache_destroy(flow_cache);
432 }
433