c37c8e0ff99742d8a59ad9a550540b8172a4341e
[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 bool iphdr_ok(struct sk_buff *skb)
44 {
45         int nh_ofs = skb_network_offset(skb);
46         if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
47                 int ip_len = ip_hdrlen(skb);
48                 return (ip_len >= sizeof(struct iphdr)
49                         && pskb_may_pull(skb, nh_ofs + ip_len));
50         }
51         return false;
52 }
53
54 static inline bool tcphdr_ok(struct sk_buff *skb)
55 {
56         int th_ofs = skb_transport_offset(skb);
57         if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
58                 int tcp_len = tcp_hdrlen(skb);
59                 return (tcp_len >= sizeof(struct tcphdr)
60                         && skb->len >= th_ofs + tcp_len);
61         }
62         return false;
63 }
64
65 static inline bool udphdr_ok(struct sk_buff *skb)
66 {
67         int th_ofs = skb_transport_offset(skb);
68         return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
69 }
70
71 static inline bool icmphdr_ok(struct sk_buff *skb)
72 {
73         int th_ofs = skb_transport_offset(skb);
74         return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
75 }
76
77 #define TCP_FLAGS_OFFSET 13
78 #define TCP_FLAG_MASK 0x3f
79
80 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
81 {
82         u8 tcp_flags = 0;
83
84         if (flow->key.dl_type == htons(ETH_P_IP) &&
85             flow->key.nw_proto == IPPROTO_TCP) {
86                 u8 *tcp = (u8 *)tcp_hdr(skb);
87                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
88         }
89
90         spin_lock_bh(&flow->lock);
91         flow->used = jiffies;
92         flow->packet_count++;
93         flow->byte_count += skb->len;
94         flow->tcp_flags |= tcp_flags;
95         spin_unlock_bh(&flow->lock);
96 }
97
98 struct sw_flow_actions *flow_actions_alloc(size_t n_actions)
99 {
100         struct sw_flow_actions *sfa;
101
102         if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union odp_action))
103                 return ERR_PTR(-EINVAL);
104
105         sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
106                       GFP_KERNEL);
107         if (!sfa)
108                 return ERR_PTR(-ENOMEM);
109
110         sfa->n_actions = n_actions;
111         return sfa;
112 }
113
114
115 /* Frees 'flow' immediately. */
116 static void flow_free(struct sw_flow *flow)
117 {
118         if (unlikely(!flow))
119                 return;
120         kfree(flow->sf_acts);
121         kmem_cache_free(flow_cache, flow);
122 }
123
124 void flow_free_tbl(struct tbl_node *node)
125 {
126         struct sw_flow *flow = flow_cast(node);
127         flow_free(flow);
128 }
129
130 /* RCU callback used by flow_deferred_free. */
131 static void rcu_free_flow_callback(struct rcu_head *rcu)
132 {
133         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
134         flow_free(flow);
135 }
136
137 /* Schedules 'flow' to be freed after the next RCU grace period.
138  * The caller must hold rcu_read_lock for this to be sensible. */
139 void flow_deferred_free(struct sw_flow *flow)
140 {
141         call_rcu(&flow->rcu, rcu_free_flow_callback);
142 }
143
144 /* RCU callback used by flow_deferred_free_acts. */
145 static void rcu_free_acts_callback(struct rcu_head *rcu)
146 {
147         struct sw_flow_actions *sf_acts = container_of(rcu, 
148                         struct sw_flow_actions, rcu);
149         kfree(sf_acts);
150 }
151
152 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
153  * The caller must hold rcu_read_lock for this to be sensible. */
154 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
155 {
156         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
157 }
158
159 static void parse_vlan(struct sk_buff *skb, struct odp_flow_key *key)
160 {
161         struct qtag_prefix {
162                 __be16 eth_type; /* ETH_P_8021Q */
163                 __be16 tci;
164         };
165         struct qtag_prefix *qp;
166
167         if (skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))
168                 return;
169
170         qp = (struct qtag_prefix *) skb->data;
171         key->dl_vlan = qp->tci & htons(VLAN_VID_MASK);
172         key->dl_vlan_pcp = (ntohs(qp->tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
173         __skb_pull(skb, sizeof(struct qtag_prefix));
174 }
175
176 static __be16 parse_ethertype(struct sk_buff *skb)
177 {
178         struct llc_snap_hdr {
179                 u8  dsap;  /* Always 0xAA */
180                 u8  ssap;  /* Always 0xAA */
181                 u8  ctrl;
182                 u8  oui[3];
183                 u16 ethertype;
184         };
185         struct llc_snap_hdr *llc;
186         __be16 proto;
187
188         proto = *(__be16 *) skb->data;
189         __skb_pull(skb, sizeof(__be16));
190
191         if (ntohs(proto) >= ODP_DL_TYPE_ETH2_CUTOFF)
192                 return proto;
193
194         if (unlikely(skb->len < sizeof(struct llc_snap_hdr)))
195                 return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
196
197         llc = (struct llc_snap_hdr *) skb->data;
198         if (llc->dsap != LLC_SAP_SNAP ||
199             llc->ssap != LLC_SAP_SNAP ||
200             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
201                 return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
202
203         __skb_pull(skb, sizeof(struct llc_snap_hdr));
204         return llc->ethertype;
205 }
206
207 /**
208  * flow_extract - extracts a flow key from an Ethernet frame.
209  * @skb: sk_buff that contains the frame, with skb->data pointing to the
210  * Ethernet header
211  * @in_port: port number on which @skb was received.
212  * @key: output flow key
213  *
214  * The caller must ensure that skb->len >= ETH_HLEN.
215  *
216  * Sets OVS_CB(skb)->is_frag to %true if @skb is an IPv4 fragment, otherwise to
217  * %false.
218  */
219 int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key)
220 {
221         struct ethhdr *eth;
222
223         memset(key, 0, sizeof *key);
224         key->tun_id = OVS_CB(skb)->tun_id;
225         key->in_port = in_port;
226         key->dl_vlan = htons(ODP_VLAN_NONE);
227         OVS_CB(skb)->is_frag = false;
228
229         if (!pskb_may_pull(skb, min(skb->len, 64u)))
230                 return 0;
231
232         skb_reset_mac_header(skb);
233
234         /* Link layer. */
235         eth = eth_hdr(skb);
236         memcpy(key->dl_src, eth->h_source, ETH_ALEN);
237         memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
238
239         /* dl_type, dl_vlan, dl_vlan_pcp. */
240         __skb_pull(skb, 2 * ETH_ALEN);
241         if (eth->h_proto == htons(ETH_P_8021Q))
242                 parse_vlan(skb, key);
243         key->dl_type = parse_ethertype(skb);
244         skb_reset_network_header(skb);
245         __skb_push(skb, skb->data - (unsigned char *)eth);
246
247         /* Network layer. */
248         if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
249                 struct iphdr *nh = ip_hdr(skb);
250                 int th_ofs = skb_network_offset(skb) + nh->ihl * 4;
251                 key->nw_src = nh->saddr;
252                 key->nw_dst = nh->daddr;
253                 key->nw_tos = nh->tos & ~INET_ECN_MASK;
254                 key->nw_proto = nh->protocol;
255                 skb_set_transport_header(skb, th_ofs);
256
257                 /* Transport layer. */
258                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
259                         if (key->nw_proto == IPPROTO_TCP) {
260                                 if (tcphdr_ok(skb)) {
261                                         struct tcphdr *tcp = tcp_hdr(skb);
262                                         key->tp_src = tcp->source;
263                                         key->tp_dst = tcp->dest;
264                                 } else {
265                                         /* Avoid tricking other code into
266                                          * thinking that this packet has an L4
267                                          * header. */
268                                         key->nw_proto = 0;
269                                 }
270                         } else if (key->nw_proto == IPPROTO_UDP) {
271                                 if (udphdr_ok(skb)) {
272                                         struct udphdr *udp = udp_hdr(skb);
273                                         key->tp_src = udp->source;
274                                         key->tp_dst = udp->dest;
275                                 } else {
276                                         /* Avoid tricking other code into
277                                          * thinking that this packet has an L4
278                                          * header. */
279                                         key->nw_proto = 0;
280                                 }
281                         } else if (key->nw_proto == IPPROTO_ICMP) {
282                                 if (icmphdr_ok(skb)) {
283                                         struct icmphdr *icmp = icmp_hdr(skb);
284                                         /* The ICMP type and code fields use the 16-bit
285                                          * transport port fields, so we need to store them
286                                          * in 16-bit network byte order. */
287                                         key->tp_src = htons(icmp->type);
288                                         key->tp_dst = htons(icmp->code);
289                                 } else {
290                                         /* Avoid tricking other code into
291                                          * thinking that this packet has an L4
292                                          * header. */
293                                         key->nw_proto = 0;
294                                 }
295                         }
296                 } else {
297                         OVS_CB(skb)->is_frag = true;
298                 }
299         } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
300                 struct arp_eth_header *arp;
301
302                 arp = (struct arp_eth_header *)skb_network_header(skb);
303
304                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
305                                 && arp->ar_pro == htons(ETH_P_IP)
306                                 && arp->ar_hln == ETH_ALEN
307                                 && arp->ar_pln == 4) {
308
309                         /* We only match on the lower 8 bits of the opcode. */
310                         if (ntohs(arp->ar_op) <= 0xff) {
311                                 key->nw_proto = ntohs(arp->ar_op);
312                         }
313
314                         if (key->nw_proto == ARPOP_REQUEST 
315                                         || key->nw_proto == ARPOP_REPLY) {
316                                 memcpy(&key->nw_src, arp->ar_sip, sizeof(key->nw_src));
317                                 memcpy(&key->nw_dst, arp->ar_tip, sizeof(key->nw_dst));
318                         }
319                 }
320         } else {
321                 skb_reset_transport_header(skb);
322         }
323         return 0;
324 }
325
326 u32 flow_hash(const struct odp_flow_key *key)
327 {
328         return jhash2((u32*)key, sizeof *key / sizeof(u32), hash_seed);
329 }
330
331 int flow_cmp(const struct tbl_node *node, void *key2_)
332 {
333         const struct odp_flow_key *key1 = &flow_cast(node)->key;
334         const struct odp_flow_key *key2 = key2_;
335
336         return !memcmp(key1, key2, sizeof(struct odp_flow_key));
337 }
338
339 /* Initializes the flow module.
340  * Returns zero if successful or a negative error code. */
341 int flow_init(void)
342 {
343         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
344                                         0, NULL);
345         if (flow_cache == NULL)
346                 return -ENOMEM;
347
348         get_random_bytes(&hash_seed, sizeof hash_seed);
349
350         return 0;
351 }
352
353 /* Uninitializes the flow module. */
354 void flow_exit(void)
355 {
356         kmem_cache_destroy(flow_cache);
357 }