3b95e3bbcc135c03abca0ae9e3714f3a895be5c2
[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/jiffies.h>
18 #include <linux/llc.h>
19 #include <linux/module.h>
20 #include <linux/in.h>
21 #include <linux/rcupdate.h>
22 #include <linux/if_arp.h>
23 #include <linux/if_ether.h>
24 #include <linux/ip.h>
25 #include <linux/tcp.h>
26 #include <linux/udp.h>
27 #include <linux/icmp.h>
28 #include <net/ip.h>
29
30 #include "compat.h"
31
32 struct kmem_cache *flow_cache;
33
34 struct arp_eth_header
35 {
36         __be16      ar_hrd;     /* format of hardware address   */
37         __be16      ar_pro;     /* format of protocol address   */
38         unsigned char   ar_hln; /* length of hardware address   */
39         unsigned char   ar_pln; /* length of protocol address   */
40         __be16      ar_op;      /* ARP opcode (command)     */
41
42         /* Ethernet+IPv4 specific members. */
43         unsigned char       ar_sha[ETH_ALEN];   /* sender hardware address  */
44         unsigned char       ar_sip[4];          /* sender IP address        */
45         unsigned char       ar_tha[ETH_ALEN];   /* target hardware address  */
46         unsigned char       ar_tip[4];          /* target IP address        */
47 } __attribute__((packed));
48
49 static inline int arphdr_ok(struct sk_buff *skb)
50 {
51         int nh_ofs = skb_network_offset(skb);
52         return pskb_may_pull(skb, nh_ofs + sizeof(struct arp_eth_header));
53 }
54
55 static inline int iphdr_ok(struct sk_buff *skb)
56 {
57         int nh_ofs = skb_network_offset(skb);
58         if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
59                 int ip_len = ip_hdrlen(skb);
60                 return (ip_len >= sizeof(struct iphdr)
61                         && pskb_may_pull(skb, nh_ofs + ip_len));
62         }
63         return 0;
64 }
65
66 static inline int 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 0;
75 }
76
77 static inline int 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 int 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 static inline struct ovs_tcphdr *ovs_tcp_hdr(const struct sk_buff *skb)
93 {
94         return (struct ovs_tcphdr *)skb_transport_header(skb);
95 }
96
97 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
98 {
99         unsigned long flags;
100         u8 tcp_flags = 0;
101
102         if (flow->key.dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
103                 struct iphdr *nh = ip_hdr(skb);
104                 flow->ip_tos = nh->tos;
105                 if (flow->key.nw_proto == IPPROTO_TCP && tcphdr_ok(skb)) {
106                         u8 *tcp = (u8 *)tcp_hdr(skb);
107                         tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
108                 }
109         }
110
111         spin_lock_irqsave(&flow->lock, flags);
112         getnstimeofday(&flow->used);
113         flow->packet_count++;
114         flow->byte_count += skb->len;
115         flow->tcp_flags |= tcp_flags;
116         spin_unlock_irqrestore(&flow->lock, flags);
117 }
118
119 struct sw_flow_actions *flow_actions_alloc(size_t n_actions)
120 {
121         struct sw_flow_actions *sfa;
122
123         if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union odp_action))
124                 return ERR_PTR(-EINVAL);
125
126         sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
127                       GFP_KERNEL);
128         if (!sfa)
129                 return ERR_PTR(-ENOMEM);
130
131         sfa->n_actions = n_actions;
132         return sfa;
133 }
134
135
136 /* Frees 'flow' immediately. */
137 void flow_free(struct sw_flow *flow)
138 {
139         if (unlikely(!flow))
140                 return;
141         kfree(flow->sf_acts);
142         kmem_cache_free(flow_cache, flow);
143 }
144
145 /* RCU callback used by flow_deferred_free. */
146 static void rcu_free_flow_callback(struct rcu_head *rcu)
147 {
148         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
149         flow_free(flow);
150 }
151
152 /* Schedules 'flow' 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(struct sw_flow *flow)
155 {
156         call_rcu(&flow->rcu, rcu_free_flow_callback);
157 }
158
159 /* RCU callback used by flow_deferred_free_acts. */
160 static void rcu_free_acts_callback(struct rcu_head *rcu)
161 {
162         struct sw_flow_actions *sf_acts = container_of(rcu, 
163                         struct sw_flow_actions, rcu);
164         kfree(sf_acts);
165 }
166
167 /* Schedules 'sf_acts' 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_acts(struct sw_flow_actions *sf_acts)
170 {
171         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
172 }
173
174 #define SNAP_OUI_LEN 3
175
176 struct eth_snap_hdr
177 {
178         struct ethhdr eth;
179         u8  dsap;  /* Always 0xAA */
180         u8  ssap;  /* Always 0xAA */
181         u8  ctrl;
182         u8  oui[SNAP_OUI_LEN];
183         u16 ethertype;
184 } __attribute__ ((packed));
185
186 static int is_snap(const struct eth_snap_hdr *esh)
187 {
188         return (esh->dsap == LLC_SAP_SNAP
189                 && esh->ssap == LLC_SAP_SNAP
190                 && !memcmp(esh->oui, "\0\0\0", 3));
191 }
192
193 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
194  * and initializes 'key' to match.  Returns 1 if 'skb' contains an IP
195  * fragment, 0 otherwise. */
196 int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key)
197 {
198         struct ethhdr *eth;
199         struct eth_snap_hdr *esh;
200         int retval = 0;
201         int nh_ofs;
202
203         memset(key, 0, sizeof *key);
204         key->dl_vlan = htons(ODP_VLAN_NONE);
205         key->in_port = in_port;
206
207         if (skb->len < sizeof *eth)
208                 return 0;
209         if (!pskb_may_pull(skb, skb->len >= 64 ? 64 : skb->len)) {
210                 return 0;
211         }
212
213         skb_reset_mac_header(skb);
214         eth = eth_hdr(skb);
215         esh = (struct eth_snap_hdr *) eth;
216         nh_ofs = sizeof *eth;
217         if (likely(ntohs(eth->h_proto) >= ODP_DL_TYPE_ETH2_CUTOFF))
218                 key->dl_type = eth->h_proto;
219         else if (skb->len >= sizeof *esh && is_snap(esh)) {
220                 key->dl_type = esh->ethertype;
221                 nh_ofs = sizeof *esh;
222         } else {
223                 key->dl_type = htons(ODP_DL_TYPE_NOT_ETH_TYPE);
224                 if (skb->len >= nh_ofs + sizeof(struct llc_pdu_un)) {
225                         nh_ofs += sizeof(struct llc_pdu_un); 
226                 }
227         }
228
229         /* Check for a VLAN tag */
230         if (key->dl_type == htons(ETH_P_8021Q) &&
231             skb->len >= nh_ofs + sizeof(struct vlan_hdr)) {
232                 struct vlan_hdr *vh = (struct vlan_hdr*)(skb->data + nh_ofs);
233                 key->dl_type = vh->h_vlan_encapsulated_proto;
234                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
235                 key->dl_vlan_pcp = (ntohs(vh->h_vlan_TCI) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
236                 nh_ofs += sizeof(struct vlan_hdr);
237         }
238         memcpy(key->dl_src, eth->h_source, ETH_ALEN);
239         memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
240         skb_set_network_header(skb, nh_ofs);
241
242         /* Network layer. */
243         if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
244                 struct iphdr *nh = ip_hdr(skb);
245                 int th_ofs = nh_ofs + nh->ihl * 4;
246                 key->nw_src = nh->saddr;
247                 key->nw_dst = nh->daddr;
248                 key->nw_tos = nh->tos & ~INET_ECN_MASK;
249                 key->nw_proto = nh->protocol;
250                 skb_set_transport_header(skb, th_ofs);
251
252                 /* Transport layer. */
253                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
254                         if (key->nw_proto == IPPROTO_TCP) {
255                                 if (tcphdr_ok(skb)) {
256                                         struct tcphdr *tcp = tcp_hdr(skb);
257                                         key->tp_src = tcp->source;
258                                         key->tp_dst = tcp->dest;
259                                 } else {
260                                         /* Avoid tricking other code into
261                                          * thinking that this packet has an L4
262                                          * header. */
263                                         key->nw_proto = 0;
264                                 }
265                         } else if (key->nw_proto == IPPROTO_UDP) {
266                                 if (udphdr_ok(skb)) {
267                                         struct udphdr *udp = udp_hdr(skb);
268                                         key->tp_src = udp->source;
269                                         key->tp_dst = udp->dest;
270                                 } else {
271                                         /* Avoid tricking other code into
272                                          * thinking that this packet has an L4
273                                          * header. */
274                                         key->nw_proto = 0;
275                                 }
276                         } else if (key->nw_proto == IPPROTO_ICMP) {
277                                 if (icmphdr_ok(skb)) {
278                                         struct icmphdr *icmp = icmp_hdr(skb);
279                                         /* The ICMP type and code fields use the 16-bit
280                                          * transport port fields, so we need to store them
281                                          * in 16-bit network byte order. */
282                                         key->tp_src = htons(icmp->type);
283                                         key->tp_dst = htons(icmp->code);
284                                 } else {
285                                         /* Avoid tricking other code into
286                                          * thinking that this packet has an L4
287                                          * header. */
288                                         key->nw_proto = 0;
289                                 }
290                         }
291                 } else {
292                         retval = 1;
293                 }
294         } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
295                 struct arp_eth_header *arp;
296
297                 arp = (struct arp_eth_header *)skb_network_header(skb);
298
299                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
300                                 && arp->ar_pro == htons(ETH_P_IP)
301                                 && arp->ar_hln == ETH_ALEN
302                                 && arp->ar_pln == 4) {
303
304                         /* We only match on the lower 8 bits of the opcode. */
305                         if (ntohs(arp->ar_op) <= 0xff) {
306                                 key->nw_proto = ntohs(arp->ar_op);
307                         }
308
309                         if (key->nw_proto == ARPOP_REQUEST 
310                                         || key->nw_proto == ARPOP_REPLY) {
311                                 memcpy(&key->nw_src, arp->ar_sip, sizeof(key->nw_src));
312                                 memcpy(&key->nw_dst, arp->ar_tip, sizeof(key->nw_dst));
313                         }
314                 }
315         } else {
316                 skb_reset_transport_header(skb);
317         }
318         return retval;
319 }
320
321 /* Initializes the flow module.
322  * Returns zero if successful or a negative error code. */
323 int flow_init(void)
324 {
325         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
326                                         0, NULL);
327         if (flow_cache == NULL)
328                 return -ENOMEM;
329
330         return 0;
331 }
332
333 /* Uninitializes the flow module. */
334 void flow_exit(void)
335 {
336         kmem_cache_destroy(flow_cache);
337 }