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