Implement OFPC_FRAG_DROP fragment handling policy.
[openvswitch] / 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/module.h>
16 #include <linux/tcp.h>
17 #include <linux/udp.h>
18 #include <linux/in.h>
19 #include <linux/rcupdate.h>
20 #include <net/ip.h>
21
22 #include "openflow.h"
23 #include "compat.h"
24 #include "snap.h"
25
26 struct kmem_cache *flow_cache;
27
28 /* Internal function used to compare fields in flow. */
29 static inline
30 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
31                 uint16_t w)
32 {
33         return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
34                 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
35                 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
36                 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
37                 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
38                 && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
39                 && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
40                 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
41                 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
42                 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
43 }
44
45 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
46  * modulo wildcards, zero otherwise. */
47 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
48 {
49         return flow_fields_match(a, b, (a->wildcards | b->wildcards));
50 }
51 EXPORT_SYMBOL(flow_matches);
52
53 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key 
54  * describing the deletion) match, that is, if their fields are 
55  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
56  * wildcards must match in both 't_key' and 'd_key'.  Note that the
57  * table's wildcards are ignored unless 'strict' is set. */
58 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
59 {
60         if (strict && (t->wildcards != d->wildcards))
61                 return 0;
62
63         return flow_fields_match(t, d, d->wildcards);
64 }
65 EXPORT_SYMBOL(flow_del_matches);
66
67 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
68 {
69         to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
70         memset(to->pad, '\0', sizeof(to->pad));
71         to->in_port = from->in_port;
72         to->dl_vlan = from->dl_vlan;
73         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
74         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
75         to->dl_type = from->dl_type;
76
77         to->nw_src = to->nw_dst = to->nw_proto = 0;
78         to->tp_src = to->tp_dst = 0;
79
80 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
81 #define OFPFW_NW (OFPFW_NW_SRC | OFPFW_NW_DST | OFPFW_NW_PROTO)
82         if (to->wildcards & OFPFW_DL_TYPE) {
83                 /* Can't sensibly match on network or transport headers if the
84                  * data link type is unknown. */
85                 to->wildcards |= OFPFW_NW | OFPFW_TP;
86         } else if (from->dl_type == htons(ETH_P_IP)) {
87                 to->nw_src   = from->nw_src;
88                 to->nw_dst   = from->nw_dst;
89                 to->nw_proto = from->nw_proto;
90
91                 if (to->wildcards & OFPFW_NW_PROTO) {
92                         /* Can't sensibly match on transport headers if the
93                          * network protocol is unknown. */
94                         to->wildcards |= OFPFW_TP;
95                 } else if (from->nw_proto == IPPROTO_TCP
96                            || from->nw_proto == IPPROTO_UDP) {
97                         to->tp_src = from->tp_src;
98                         to->tp_dst = from->tp_dst;
99                 } else {
100                         /* Transport layer fields are undefined.  Mark them as
101                          * exact-match to allow such flows to reside in
102                          * table-hash, instead of falling into table-linear. */
103                         to->wildcards &= ~OFPFW_TP;
104                 }
105         } else {
106                 /* Network and transport layer fields are undefined.  Mark them
107                  * as exact-match to allow such flows to reside in table-hash,
108                  * instead of falling into table-linear. */
109                 to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
110         }
111 }
112
113 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
114 {
115         to->wildcards = htons(from->wildcards);
116         to->in_port   = from->in_port;
117         to->dl_vlan   = from->dl_vlan;
118         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
119         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
120         to->dl_type   = from->dl_type;
121         to->nw_src        = from->nw_src;
122         to->nw_dst        = from->nw_dst;
123         to->nw_proto  = from->nw_proto;
124         to->tp_src        = from->tp_src;
125         to->tp_dst        = from->tp_dst;
126         memset(to->pad, '\0', sizeof(to->pad));
127 }
128
129 /* Allocates and returns a new flow with 'n_actions' action, using allocation
130  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
131 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
132 {
133         struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
134         if (unlikely(!flow))
135                 return NULL;
136
137         flow->n_actions = n_actions;
138         flow->actions = kmalloc(n_actions * sizeof *flow->actions,
139                                 flags);
140         if (unlikely(!flow->actions) && n_actions > 0) {
141                 kmem_cache_free(flow_cache, flow);
142                 return NULL;
143         }
144         return flow;
145 }
146
147 /* Frees 'flow' immediately. */
148 void flow_free(struct sw_flow *flow)
149 {
150         if (unlikely(!flow))
151                 return;
152         kfree(flow->actions);
153         kmem_cache_free(flow_cache, flow);
154 }
155 EXPORT_SYMBOL(flow_free);
156
157 /* RCU callback used by flow_deferred_free. */
158 static void rcu_callback(struct rcu_head *rcu)
159 {
160         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
161         flow_free(flow);
162 }
163
164 /* Schedules 'flow' to be freed after the next RCU grace period.
165  * The caller must hold rcu_read_lock for this to be sensible. */
166 void flow_deferred_free(struct sw_flow *flow)
167 {
168         call_rcu(&flow->rcu, rcu_callback);
169 }
170 EXPORT_SYMBOL(flow_deferred_free);
171
172 /* Prints a representation of 'key' to the kernel log. */
173 void print_flow(const struct sw_flow_key *key)
174 {
175         printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
176                         "->%02x:%02x:%02x:%02x:%02x:%02x "
177                         "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
178                         key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
179                         key->dl_src[0], key->dl_src[1], key->dl_src[2],
180                         key->dl_src[3], key->dl_src[4], key->dl_src[5],
181                         key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
182                         key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
183                         ntohs(key->dl_type),
184                         ((unsigned char *)&key->nw_src)[0],
185                         ((unsigned char *)&key->nw_src)[1],
186                         ((unsigned char *)&key->nw_src)[2],
187                         ((unsigned char *)&key->nw_src)[3],
188                         ((unsigned char *)&key->nw_dst)[0],
189                         ((unsigned char *)&key->nw_dst)[1],
190                         ((unsigned char *)&key->nw_dst)[2],
191                         ((unsigned char *)&key->nw_dst)[3],
192                         ntohs(key->tp_src), ntohs(key->tp_dst));
193 }
194 EXPORT_SYMBOL(print_flow);
195
196 static int tcphdr_ok(struct sk_buff *skb)
197 {
198         int th_ofs = skb_transport_offset(skb);
199         if (skb->len >= th_ofs + sizeof(struct tcphdr)) {
200                 int tcp_len = tcp_hdrlen(skb);
201                 return (tcp_len >= sizeof(struct tcphdr)
202                         && skb->len >= th_ofs + tcp_len);
203         }
204         return 0;
205 }
206
207 static int udphdr_ok(struct sk_buff *skb)
208 {
209         int th_ofs = skb_transport_offset(skb);
210         return skb->len >= th_ofs + sizeof(struct udphdr);
211 }
212
213 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
214  * and initializes 'key' to match.  Returns 1 if 'skb' contains an IP
215  * fragment, 0 otherwise. */
216 int flow_extract(struct sk_buff *skb, uint16_t in_port,
217                  struct sw_flow_key *key)
218 {
219         struct ethhdr *mac;
220         int nh_ofs, th_ofs;
221         int retval = 0;
222
223         key->in_port = htons(in_port);
224         key->wildcards = 0;
225         memset(key->pad, '\0', sizeof(key->pad));
226
227         /* This code doesn't check that skb->len is long enough to contain the
228          * MAC or network header.  With a 46-byte minimum length frame this
229          * assumption is always correct. */
230
231         /* Doesn't verify checksums.  Should it? */
232
233         /* Data link layer.  We only support Ethernet. */
234         mac = eth_hdr(skb);
235         nh_ofs = sizeof(struct ethhdr);
236         if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
237                 /* This is an Ethernet II frame */
238                 key->dl_type = mac->h_proto;
239         } else {
240                 /* This is an 802.2 frame */
241                 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
242                         nh_ofs += sizeof(struct snap_hdr);
243                 } else {
244                         key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
245                         nh_ofs += sizeof(struct llc_pdu_un);
246                 }
247         }
248
249         /* Check for a VLAN tag */
250         if (likely(key->dl_type != htons(ETH_P_8021Q))) {
251                 key->dl_vlan = htons(OFP_VLAN_NONE);
252         } else {
253                 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
254                 key->dl_type = vh->h_vlan_encapsulated_proto;
255                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
256                 nh_ofs += sizeof(*vh);
257         }
258         memcpy(key->dl_src, mac->h_source, ETH_ALEN);
259         memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
260         skb_set_network_header(skb, nh_ofs);
261
262         /* Network layer. */
263         if (likely(key->dl_type == htons(ETH_P_IP))) {
264                 struct iphdr *nh = ip_hdr(skb);
265                 key->nw_src = nh->saddr;
266                 key->nw_dst = nh->daddr;
267                 key->nw_proto = nh->protocol;
268                 th_ofs = nh_ofs + nh->ihl * 4;
269                 skb_set_transport_header(skb, th_ofs);
270
271                 /* Transport layer. */
272                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
273                         if (key->nw_proto == IPPROTO_TCP) {
274                                 if (tcphdr_ok(skb)) {
275                                         struct tcphdr *tcp = tcp_hdr(skb);
276                                         key->tp_src = tcp->source;
277                                         key->tp_dst = tcp->dest;
278                                 } else {
279                                         /* Avoid tricking other code into
280                                          * thinking that this packet has an L4
281                                          * header. */
282                                         goto no_proto;
283                                 }
284                         } else if (key->nw_proto == IPPROTO_UDP) {
285                                 if (udphdr_ok(skb)) {
286                                         struct udphdr *udp = udp_hdr(skb);
287                                         key->tp_src = udp->source;
288                                         key->tp_dst = udp->dest;
289                                 } else {
290                                         /* Avoid tricking other code into
291                                          * thinking that this packet has an L4
292                                          * header. */
293                                         goto no_proto;
294                                 }
295                         }
296                 } else {
297                         retval = 1;
298                         goto no_th;
299                 }
300
301                 return 0;
302         }
303
304         key->nw_src = 0;
305         key->nw_dst = 0;
306
307 no_proto:
308         key->nw_proto = 0;
309
310 no_th:
311         key->tp_src = 0;
312         key->tp_dst = 0;
313         return retval;
314 }
315
316 /* Initializes the flow module.
317  * Returns zero if successful or a negative error code. */
318 int flow_init(void)
319 {
320         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
321                                         0, NULL);
322         if (flow_cache == NULL)
323                 return -ENOMEM;
324
325         return 0;
326 }
327
328 /* Uninitializes the flow module. */
329 void flow_exit(void)
330 {
331         kmem_cache_destroy(flow_cache);
332 }
333