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