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