datapath: Report memory allocation errors in flow_extract().
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008, 2009, 2010 Nicira Networks.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include "flow.h"
10 #include "datapath.h"
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_vlan.h>
15 #include <net/llc_pdu.h>
16 #include <linux/kernel.h>
17 #include <linux/jhash.h>
18 #include <linux/jiffies.h>
19 #include <linux/llc.h>
20 #include <linux/module.h>
21 #include <linux/in.h>
22 #include <linux/rcupdate.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
25 #include <linux/ip.h>
26 #include <linux/tcp.h>
27 #include <linux/udp.h>
28 #include <linux/icmp.h>
29 #include <net/inet_ecn.h>
30 #include <net/ip.h>
31
32 #include "compat.h"
33
34 struct kmem_cache *flow_cache;
35 static unsigned int hash_seed;
36
37 static inline bool arphdr_ok(struct sk_buff *skb)
38 {
39         int nh_ofs = skb_network_offset(skb);
40         return pskb_may_pull(skb, nh_ofs + sizeof(struct arp_eth_header));
41 }
42
43 static inline int check_iphdr(struct sk_buff *skb)
44 {
45         unsigned int nh_ofs = skb_network_offset(skb);
46         unsigned int ip_len;
47
48         if (skb->len < nh_ofs + sizeof(struct iphdr))
49                 return -EINVAL;
50
51         ip_len = ip_hdrlen(skb);
52         if (ip_len < sizeof(struct iphdr) || skb->len < nh_ofs + ip_len)
53                 return -EINVAL;
54
55         /*
56          * Pull enough header bytes to account for the IP header plus the
57          * longest transport header that we parse, currently 20 bytes for TCP.
58          */
59         if (!pskb_may_pull(skb, min(nh_ofs + ip_len + 20, skb->len)))
60                 return -ENOMEM;
61
62         skb_set_transport_header(skb, nh_ofs + ip_len);
63         return 0;
64 }
65
66 static inline bool tcphdr_ok(struct sk_buff *skb)
67 {
68         int th_ofs = skb_transport_offset(skb);
69         if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
70                 int tcp_len = tcp_hdrlen(skb);
71                 return (tcp_len >= sizeof(struct tcphdr)
72                         && skb->len >= th_ofs + tcp_len);
73         }
74         return false;
75 }
76
77 static inline bool udphdr_ok(struct sk_buff *skb)
78 {
79         int th_ofs = skb_transport_offset(skb);
80         return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
81 }
82
83 static inline bool icmphdr_ok(struct sk_buff *skb)
84 {
85         int th_ofs = skb_transport_offset(skb);
86         return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
87 }
88
89 #define TCP_FLAGS_OFFSET 13
90 #define TCP_FLAG_MASK 0x3f
91
92 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
93 {
94         u8 tcp_flags = 0;
95
96         if (flow->key.dl_type == htons(ETH_P_IP) &&
97             flow->key.nw_proto == IPPROTO_TCP) {
98                 u8 *tcp = (u8 *)tcp_hdr(skb);
99                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
100         }
101
102         spin_lock_bh(&flow->lock);
103         flow->used = jiffies;
104         flow->packet_count++;
105         flow->byte_count += skb->len;
106         flow->tcp_flags |= tcp_flags;
107         spin_unlock_bh(&flow->lock);
108 }
109
110 struct sw_flow_actions *flow_actions_alloc(size_t n_actions)
111 {
112         struct sw_flow_actions *sfa;
113
114         if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union odp_action))
115                 return ERR_PTR(-EINVAL);
116
117         sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
118                       GFP_KERNEL);
119         if (!sfa)
120                 return ERR_PTR(-ENOMEM);
121
122         sfa->n_actions = n_actions;
123         return sfa;
124 }
125
126
127 /* Frees 'flow' immediately. */
128 static void flow_free(struct sw_flow *flow)
129 {
130         if (unlikely(!flow))
131                 return;
132         kfree(flow->sf_acts);
133         kmem_cache_free(flow_cache, flow);
134 }
135
136 void flow_free_tbl(struct tbl_node *node)
137 {
138         struct sw_flow *flow = flow_cast(node);
139         flow_free(flow);
140 }
141
142 /* RCU callback used by flow_deferred_free. */
143 static void rcu_free_flow_callback(struct rcu_head *rcu)
144 {
145         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
146         flow_free(flow);
147 }
148
149 /* Schedules 'flow' to be freed after the next RCU grace period.
150  * The caller must hold rcu_read_lock for this to be sensible. */
151 void flow_deferred_free(struct sw_flow *flow)
152 {
153         call_rcu(&flow->rcu, rcu_free_flow_callback);
154 }
155
156 /* RCU callback used by flow_deferred_free_acts. */
157 static void rcu_free_acts_callback(struct rcu_head *rcu)
158 {
159         struct sw_flow_actions *sf_acts = container_of(rcu, 
160                         struct sw_flow_actions, rcu);
161         kfree(sf_acts);
162 }
163
164 /* Schedules 'sf_acts' 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_acts(struct sw_flow_actions *sf_acts)
167 {
168         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
169 }
170
171 static void parse_vlan(struct sk_buff *skb, struct odp_flow_key *key)
172 {
173         struct qtag_prefix {
174                 __be16 eth_type; /* ETH_P_8021Q */
175                 __be16 tci;
176         };
177         struct qtag_prefix *qp;
178
179         if (skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))
180                 return;
181
182         qp = (struct qtag_prefix *) skb->data;
183         key->dl_vlan = qp->tci & htons(VLAN_VID_MASK);
184         key->dl_vlan_pcp = (ntohs(qp->tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
185         __skb_pull(skb, sizeof(struct qtag_prefix));
186 }
187
188 static __be16 parse_ethertype(struct sk_buff *skb)
189 {
190         struct llc_snap_hdr {
191                 u8  dsap;  /* Always 0xAA */
192                 u8  ssap;  /* Always 0xAA */
193                 u8  ctrl;
194                 u8  oui[3];
195                 u16 ethertype;
196         };
197         struct llc_snap_hdr *llc;
198         __be16 proto;
199
200         proto = *(__be16 *) skb->data;
201         __skb_pull(skb, sizeof(__be16));
202
203         if (ntohs(proto) >= ODP_DL_TYPE_ETH2_CUTOFF)
204                 return proto;
205
206         if (unlikely(skb->len < sizeof(struct llc_snap_hdr)))
207                 return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
208
209         llc = (struct llc_snap_hdr *) skb->data;
210         if (llc->dsap != LLC_SAP_SNAP ||
211             llc->ssap != LLC_SAP_SNAP ||
212             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
213                 return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
214
215         __skb_pull(skb, sizeof(struct llc_snap_hdr));
216         return llc->ethertype;
217 }
218
219 /**
220  * flow_extract - extracts a flow key from an Ethernet frame.
221  * @skb: sk_buff that contains the frame, with skb->data pointing to the
222  * Ethernet header
223  * @in_port: port number on which @skb was received.
224  * @key: output flow key
225  *
226  * The caller must ensure that skb->len >= ETH_HLEN.
227  *
228  * Returns 0 if successful, otherwise a negative errno value.
229  *
230  * Sets OVS_CB(skb)->is_frag to %true if @skb is an IPv4 fragment, otherwise to
231  * %false.
232  */
233 int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key)
234 {
235         struct ethhdr *eth;
236
237         memset(key, 0, sizeof *key);
238         key->tun_id = OVS_CB(skb)->tun_id;
239         key->in_port = in_port;
240         key->dl_vlan = htons(ODP_VLAN_NONE);
241         OVS_CB(skb)->is_frag = false;
242
243         /*
244          * We would really like to pull as many bytes as we could possibly
245          * want to parse into the linear data area.  Currently that is:
246          *
247          *    14     Ethernet header
248          *     4     VLAN header
249          *    60     max IP header with options
250          *    20     max TCP/UDP/ICMP header (don't care about options)
251          *    --
252          *    98
253          *
254          * But Xen only allocates 64 or 72 bytes for the linear data area in
255          * netback, which means that we would reallocate and copy the skb's
256          * linear data on every packet if we did that.  So instead just pull 64
257          * bytes, which is always sufficient without IP options, and then check
258          * whether we need to pull more later when we look at the IP header.
259          */
260         if (!pskb_may_pull(skb, min(skb->len, 64u)))
261                 return -ENOMEM;
262
263         skb_reset_mac_header(skb);
264
265         /* Link layer. */
266         eth = eth_hdr(skb);
267         memcpy(key->dl_src, eth->h_source, ETH_ALEN);
268         memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
269
270         /* dl_type, dl_vlan, dl_vlan_pcp. */
271         __skb_pull(skb, 2 * ETH_ALEN);
272         if (eth->h_proto == htons(ETH_P_8021Q))
273                 parse_vlan(skb, key);
274         key->dl_type = parse_ethertype(skb);
275         skb_reset_network_header(skb);
276         __skb_push(skb, skb->data - (unsigned char *)eth);
277
278         /* Network layer. */
279         if (key->dl_type == htons(ETH_P_IP)) {
280                 struct iphdr *nh;
281                 int error;
282
283                 error = check_iphdr(skb);
284                 if (unlikely(error)) {
285                         if (error == -EINVAL) {
286                                 skb->transport_header = skb->network_header;
287                                 return 0;
288                         }
289                         return error;
290                 }
291
292                 nh = ip_hdr(skb);
293                 key->nw_src = nh->saddr;
294                 key->nw_dst = nh->daddr;
295                 key->nw_tos = nh->tos & ~INET_ECN_MASK;
296                 key->nw_proto = nh->protocol;
297
298                 /* Transport layer. */
299                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
300                         if (key->nw_proto == IPPROTO_TCP) {
301                                 if (tcphdr_ok(skb)) {
302                                         struct tcphdr *tcp = tcp_hdr(skb);
303                                         key->tp_src = tcp->source;
304                                         key->tp_dst = tcp->dest;
305                                 } else {
306                                         /* Avoid tricking other code into
307                                          * thinking that this packet has an L4
308                                          * header. */
309                                         key->nw_proto = 0;
310                                 }
311                         } else if (key->nw_proto == IPPROTO_UDP) {
312                                 if (udphdr_ok(skb)) {
313                                         struct udphdr *udp = udp_hdr(skb);
314                                         key->tp_src = udp->source;
315                                         key->tp_dst = udp->dest;
316                                 } else {
317                                         /* Avoid tricking other code into
318                                          * thinking that this packet has an L4
319                                          * header. */
320                                         key->nw_proto = 0;
321                                 }
322                         } else if (key->nw_proto == IPPROTO_ICMP) {
323                                 if (icmphdr_ok(skb)) {
324                                         struct icmphdr *icmp = icmp_hdr(skb);
325                                         /* The ICMP type and code fields use the 16-bit
326                                          * transport port fields, so we need to store them
327                                          * in 16-bit network byte order. */
328                                         key->tp_src = htons(icmp->type);
329                                         key->tp_dst = htons(icmp->code);
330                                 } else {
331                                         /* Avoid tricking other code into
332                                          * thinking that this packet has an L4
333                                          * header. */
334                                         key->nw_proto = 0;
335                                 }
336                         }
337                 } else {
338                         OVS_CB(skb)->is_frag = true;
339                 }
340         } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
341                 struct arp_eth_header *arp;
342
343                 arp = (struct arp_eth_header *)skb_network_header(skb);
344
345                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
346                                 && arp->ar_pro == htons(ETH_P_IP)
347                                 && arp->ar_hln == ETH_ALEN
348                                 && arp->ar_pln == 4) {
349
350                         /* We only match on the lower 8 bits of the opcode. */
351                         if (ntohs(arp->ar_op) <= 0xff) {
352                                 key->nw_proto = ntohs(arp->ar_op);
353                         }
354
355                         if (key->nw_proto == ARPOP_REQUEST 
356                                         || key->nw_proto == ARPOP_REPLY) {
357                                 memcpy(&key->nw_src, arp->ar_sip, sizeof(key->nw_src));
358                                 memcpy(&key->nw_dst, arp->ar_tip, sizeof(key->nw_dst));
359                         }
360                 }
361         } else {
362                 skb_reset_transport_header(skb);
363         }
364         return 0;
365 }
366
367 u32 flow_hash(const struct odp_flow_key *key)
368 {
369         return jhash2((u32*)key, sizeof *key / sizeof(u32), hash_seed);
370 }
371
372 int flow_cmp(const struct tbl_node *node, void *key2_)
373 {
374         const struct odp_flow_key *key1 = &flow_cast(node)->key;
375         const struct odp_flow_key *key2 = key2_;
376
377         return !memcmp(key1, key2, sizeof(struct odp_flow_key));
378 }
379
380 /* Initializes the flow module.
381  * Returns zero if successful or a negative error code. */
382 int flow_init(void)
383 {
384         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
385                                         0, NULL);
386         if (flow_cache == NULL)
387                 return -ENOMEM;
388
389         get_random_bytes(&hash_seed, sizeof hash_seed);
390
391         return 0;
392 }
393
394 /* Uninitializes the flow module. */
395 void flow_exit(void)
396 {
397         kmem_cache_destroy(flow_cache);
398 }