Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[openvswitch] / datapath / flow.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008, 2009 Nicira Networks.
4  */
5
6 #include "flow.h"
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/if_ether.h>
10 #include <linux/if_vlan.h>
11 #include <net/llc_pdu.h>
12 #include <linux/kernel.h>
13 #include <linux/jiffies.h>
14 #include <linux/llc.h>
15 #include <linux/module.h>
16 #include <linux/in.h>
17 #include <linux/rcupdate.h>
18 #include <linux/if_ether.h>
19 #include <linux/ip.h>
20 #include <linux/tcp.h>
21 #include <linux/udp.h>
22 #include <linux/icmp.h>
23 #include <net/ip.h>
24
25 #include "compat.h"
26
27 struct kmem_cache *flow_cache;
28
29 static inline int iphdr_ok(struct sk_buff *skb)
30 {
31         int nh_ofs = skb_network_offset(skb);
32         if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
33                 int ip_len = ip_hdrlen(skb);
34                 return (ip_len >= sizeof(struct iphdr)
35                         && pskb_may_pull(skb, nh_ofs + ip_len));
36         }
37         return 0;
38 }
39
40 static inline int tcphdr_ok(struct sk_buff *skb)
41 {
42         int th_ofs = skb_transport_offset(skb);
43         if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
44                 int tcp_len = tcp_hdrlen(skb);
45                 return (tcp_len >= sizeof(struct tcphdr)
46                         && skb->len >= th_ofs + tcp_len);
47         }
48         return 0;
49 }
50
51 static inline int udphdr_ok(struct sk_buff *skb)
52 {
53         int th_ofs = skb_transport_offset(skb);
54         return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
55 }
56
57 static inline int icmphdr_ok(struct sk_buff *skb)
58 {
59         int th_ofs = skb_transport_offset(skb);
60         return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
61 }
62
63 #define TCP_FLAGS_OFFSET 13
64 #define TCP_FLAG_MASK 0x3f
65
66 static inline struct ovs_tcphdr *ovs_tcp_hdr(const struct sk_buff *skb)
67 {
68         return (struct ovs_tcphdr *)skb_transport_header(skb);
69 }
70
71 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
72 {
73         unsigned long flags;
74         u8 tcp_flags = 0;
75
76         if (flow->key.dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
77                 struct iphdr *nh = ip_hdr(skb);
78                 flow->ip_tos = nh->tos;
79                 if (flow->key.nw_proto == IPPROTO_TCP && tcphdr_ok(skb)) {
80                         u8 *tcp = (u8 *)tcp_hdr(skb);
81                         tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
82                 }
83         }
84
85         spin_lock_irqsave(&flow->lock, flags);
86         getnstimeofday(&flow->used);
87         flow->packet_count++;
88         flow->byte_count += skb->len;
89         flow->tcp_flags |= tcp_flags;
90         spin_unlock_irqrestore(&flow->lock, flags);
91 }
92
93 struct sw_flow_actions *flow_actions_alloc(size_t n_actions)
94 {
95         struct sw_flow_actions *sfa;
96
97         if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union odp_action))
98                 return ERR_PTR(-EINVAL);
99
100         sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
101                       GFP_KERNEL);
102         if (!sfa)
103                 return ERR_PTR(-ENOMEM);
104
105         sfa->n_actions = n_actions;
106         return sfa;
107 }
108
109
110 /* Frees 'flow' immediately. */
111 void flow_free(struct sw_flow *flow)
112 {
113         if (unlikely(!flow))
114                 return;
115         kfree(flow->sf_acts);
116         kmem_cache_free(flow_cache, flow);
117 }
118
119 /* RCU callback used by flow_deferred_free. */
120 static void rcu_free_flow_callback(struct rcu_head *rcu)
121 {
122         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
123         flow_free(flow);
124 }
125
126 /* Schedules 'flow' to be freed after the next RCU grace period.
127  * The caller must hold rcu_read_lock for this to be sensible. */
128 void flow_deferred_free(struct sw_flow *flow)
129 {
130         call_rcu(&flow->rcu, rcu_free_flow_callback);
131 }
132
133 /* RCU callback used by flow_deferred_free_acts. */
134 static void rcu_free_acts_callback(struct rcu_head *rcu)
135 {
136         struct sw_flow_actions *sf_acts = container_of(rcu, 
137                         struct sw_flow_actions, rcu);
138         kfree(sf_acts);
139 }
140
141 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
142  * The caller must hold rcu_read_lock for this to be sensible. */
143 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
144 {
145         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
146 }
147
148 #define SNAP_OUI_LEN 3
149
150 struct eth_snap_hdr
151 {
152         struct ethhdr eth;
153         u8  dsap;  /* Always 0xAA */
154         u8  ssap;  /* Always 0xAA */
155         u8  ctrl;
156         u8  oui[SNAP_OUI_LEN];
157         u16 ethertype;
158 } __attribute__ ((packed));
159
160 static int is_snap(const struct eth_snap_hdr *esh)
161 {
162         return (esh->dsap == LLC_SAP_SNAP
163                 && esh->ssap == LLC_SAP_SNAP
164                 && !memcmp(esh->oui, "\0\0\0", 3));
165 }
166
167 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
168  * and initializes 'key' to match.  Returns 1 if 'skb' contains an IP
169  * fragment, 0 otherwise. */
170 int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key)
171 {
172         struct ethhdr *eth;
173         struct eth_snap_hdr *esh;
174         int retval = 0;
175         int nh_ofs;
176
177         memset(key, 0, sizeof *key);
178         key->dl_vlan = htons(ODP_VLAN_NONE);
179         key->in_port = in_port;
180
181         if (skb->len < sizeof *eth)
182                 return 0;
183         if (!pskb_may_pull(skb, skb->len >= 64 ? 64 : skb->len)) {
184                 return 0;
185         }
186
187         skb_reset_mac_header(skb);
188         eth = eth_hdr(skb);
189         esh = (struct eth_snap_hdr *) eth;
190         nh_ofs = sizeof *eth;
191         if (likely(ntohs(eth->h_proto) >= ODP_DL_TYPE_ETH2_CUTOFF))
192                 key->dl_type = eth->h_proto;
193         else if (skb->len >= sizeof *esh && is_snap(esh)) {
194                 key->dl_type = esh->ethertype;
195                 nh_ofs = sizeof *esh;
196         } else {
197                 key->dl_type = htons(ODP_DL_TYPE_NOT_ETH_TYPE);
198                 if (skb->len >= nh_ofs + sizeof(struct llc_pdu_un)) {
199                         nh_ofs += sizeof(struct llc_pdu_un); 
200                 }
201         }
202
203         /* Check for a VLAN tag */
204         if (key->dl_type == htons(ETH_P_8021Q) &&
205             skb->len >= nh_ofs + sizeof(struct vlan_hdr)) {
206                 struct vlan_hdr *vh = (struct vlan_hdr*)(skb->data + nh_ofs);
207                 key->dl_type = vh->h_vlan_encapsulated_proto;
208                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
209                 nh_ofs += sizeof(struct vlan_hdr);
210         }
211         memcpy(key->dl_src, eth->h_source, ETH_ALEN);
212         memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
213         skb_set_network_header(skb, nh_ofs);
214
215         /* Network layer. */
216         if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
217                 struct iphdr *nh = ip_hdr(skb);
218                 int th_ofs = nh_ofs + nh->ihl * 4;
219                 key->nw_src = nh->saddr;
220                 key->nw_dst = nh->daddr;
221                 key->nw_proto = nh->protocol;
222                 skb_set_transport_header(skb, th_ofs);
223
224                 /* Transport layer. */
225                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
226                         if (key->nw_proto == IPPROTO_TCP) {
227                                 if (tcphdr_ok(skb)) {
228                                         struct tcphdr *tcp = tcp_hdr(skb);
229                                         key->tp_src = tcp->source;
230                                         key->tp_dst = tcp->dest;
231                                 } else {
232                                         /* Avoid tricking other code into
233                                          * thinking that this packet has an L4
234                                          * header. */
235                                         key->nw_proto = 0;
236                                 }
237                         } else if (key->nw_proto == IPPROTO_UDP) {
238                                 if (udphdr_ok(skb)) {
239                                         struct udphdr *udp = udp_hdr(skb);
240                                         key->tp_src = udp->source;
241                                         key->tp_dst = udp->dest;
242                                 } else {
243                                         /* Avoid tricking other code into
244                                          * thinking that this packet has an L4
245                                          * header. */
246                                         key->nw_proto = 0;
247                                 }
248                         } else if (key->nw_proto == IPPROTO_ICMP) {
249                                 if (icmphdr_ok(skb)) {
250                                         struct icmphdr *icmp = icmp_hdr(skb);
251                                         /* The ICMP type and code fields use the 16-bit
252                                          * transport port fields, so we need to store them
253                                          * in 16-bit network byte order. */
254                                         key->tp_src = htons(icmp->type);
255                                         key->tp_dst = htons(icmp->code);
256                                 } else {
257                                         /* Avoid tricking other code into
258                                          * thinking that this packet has an L4
259                                          * header. */
260                                         key->nw_proto = 0;
261                                 }
262                         }
263                 } else {
264                         retval = 1;
265                 }
266         } else {
267                 skb_reset_transport_header(skb);
268         }
269         return retval;
270 }
271
272 /* Initializes the flow module.
273  * Returns zero if successful or a negative error code. */
274 int flow_init(void)
275 {
276         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
277                                         0, NULL);
278         if (flow_cache == NULL)
279                 return -ENOMEM;
280
281         return 0;
282 }
283
284 /* Uninitializes the flow module. */
285 void flow_exit(void)
286 {
287         kmem_cache_destroy(flow_cache);
288 }
289
290 void print_flow(const struct odp_flow_key *key)
291 {
292 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
293 #define MAC_ARG(x) ((u8*)(x))[0],((u8*)(x))[1],((u8*)(x))[2],((u8*)(x))[3],((u8*)(x))[4],((u8*)(x))[5]
294     printk("port%04x:vlan%d mac"MAC_FMT"->"MAC_FMT" "
295            "type%04x proto%d ip%x->%x port%d->%d\n",
296            key->in_port, ntohs(key->dl_vlan),
297            MAC_ARG(key->dl_src), MAC_ARG(key->dl_dst),
298            ntohs(key->dl_type), key->nw_proto,
299            key->nw_src, key->nw_dst,
300            ntohs(key->tp_src), ntohs(key->tp_dst));
301 }