Update 2007 copyrights to include 2008.
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
4  * Stanford Junior University
5  */
6
7 #include "flow.h"
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_vlan.h>
12 #include <linux/if_arp.h>
13 #include <net/llc_pdu.h>
14 #include <linux/ip.h>
15 #include <linux/ipv6.h>
16 #include <linux/kernel.h>
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
19 #include <linux/in.h>
20 #include <linux/rcupdate.h>
21
22 #include "openflow.h"
23 #include "compat.h"
24 #include "snap.h"
25
26 struct kmem_cache *flow_cache;
27
28 /* Internal function used to compare fields in flow. */
29 static inline
30 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
31                 uint16_t w)
32 {
33         return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
34                 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
35                 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
36                 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
37                 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
38                 && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
39                 && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
40                 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
41                 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
42                 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
43 }
44
45 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
46  * modulo wildcards, zero otherwise. */
47 inline
48 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
49 {
50         return flow_fields_match(a, b, (a->wildcards | b->wildcards));
51 }
52
53 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key 
54  * describing the deletion) match, that is, if their fields are 
55  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
56  * wildcards must match in both 't_key' and 'd_key'.  Note that the
57  * table's wildcards are ignored unless 'strict' is set. */
58 inline
59 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
60 {
61         if (strict && (t->wildcards != d->wildcards))
62                 return 0;
63
64         return flow_fields_match(t, d, d->wildcards);
65 }
66
67 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
68 {
69         to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
70         to->in_port   = from->in_port;
71         to->dl_vlan   = from->dl_vlan;
72         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
73         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
74         to->dl_type   = from->dl_type;
75         to->nw_src        = from->nw_src;
76         to->nw_dst        = from->nw_dst;
77         to->nw_proto  = from->nw_proto;
78         to->tp_src        = from->tp_src;
79         to->tp_dst        = from->tp_dst;
80         memset(to->pad, '\0', sizeof(to->pad));
81 }
82
83 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
84 {
85         to->wildcards = htons(from->wildcards);
86         to->in_port   = from->in_port;
87         to->dl_vlan   = from->dl_vlan;
88         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
89         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
90         to->dl_type   = from->dl_type;
91         to->nw_src        = from->nw_src;
92         to->nw_dst        = from->nw_dst;
93         to->nw_proto  = from->nw_proto;
94         to->tp_src        = from->tp_src;
95         to->tp_dst        = from->tp_dst;
96         memset(to->pad, '\0', sizeof(to->pad));
97 }
98
99 /* Returns true if 'flow' can be deleted and set up for a deferred free, false
100  * if deletion has already been scheduled (by another thread).
101  *
102  * Caller must hold rcu_read_lock. */
103 int flow_del(struct sw_flow *flow)
104 {
105         return !atomic_cmpxchg(&flow->deleted, 0, 1);
106 }
107
108 /* Allocates and returns a new flow with 'n_actions' action, using allocation
109  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
110 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
111 {
112         struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
113         if (unlikely(!flow))
114                 return NULL;
115
116         flow->n_actions = n_actions;
117         flow->actions = kmalloc(n_actions * sizeof *flow->actions,
118                                 flags);
119         if (unlikely(!flow->actions) && n_actions > 0) {
120                 kmem_cache_free(flow_cache, flow);
121                 return NULL;
122         }
123         return flow;
124 }
125
126 /* Frees 'flow' immediately. */
127 void flow_free(struct sw_flow *flow)
128 {
129         if (unlikely(!flow))
130                 return;
131         kfree(flow->actions);
132         kmem_cache_free(flow_cache, flow);
133 }
134
135 /* RCU callback used by flow_deferred_free. */
136 static void rcu_callback(struct rcu_head *rcu)
137 {
138         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
139         flow_free(flow);
140 }
141
142 /* Schedules 'flow' to be freed after the next RCU grace period.
143  * The caller must hold rcu_read_lock for this to be sensible. */
144 void flow_deferred_free(struct sw_flow *flow)
145 {
146         call_rcu(&flow->rcu, rcu_callback);
147 }
148
149 /* Prints a representation of 'key' to the kernel log. */
150 void print_flow(const struct sw_flow_key *key)
151 {
152         printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
153                         "->%02x:%02x:%02x:%02x:%02x:%02x "
154                         "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
155                         key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
156                         key->dl_src[0], key->dl_src[1], key->dl_src[2],
157                         key->dl_src[3], key->dl_src[4], key->dl_src[5],
158                         key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
159                         key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
160                         ntohs(key->dl_type),
161                         ((unsigned char *)&key->nw_src)[0],
162                         ((unsigned char *)&key->nw_src)[1],
163                         ((unsigned char *)&key->nw_src)[2],
164                         ((unsigned char *)&key->nw_src)[3],
165                         ((unsigned char *)&key->nw_dst)[0],
166                         ((unsigned char *)&key->nw_dst)[1],
167                         ((unsigned char *)&key->nw_dst)[2],
168                         ((unsigned char *)&key->nw_dst)[3],
169                         ntohs(key->tp_src), ntohs(key->tp_dst));
170 }
171
172 uint32_t hash_in6(const struct in6_addr *in)
173 {
174         return (in->s6_addr32[0] ^ in->s6_addr32[1]
175                         ^ in->s6_addr32[2] ^ in->s6_addr32[3]);
176 }
177
178 // with inspiration from linux/if_arp.h
179 struct arp_eth_hdr {
180         uint16_t  ar_hrd;  /* format of hardware address    */
181         uint16_t  ar_pro;  /* format of protocol address    */
182         uint8_t   ar_hln;  /* length of hardware address    */
183         uint8_t   ar_pln;  /* length of protocol address    */
184         uint16_t  ar_op;   /* ARP opcode (command)          */
185
186         uint8_t   ar_sha[ETH_ALEN]; /* source hardware addr */
187         uint32_t  ar_sip;           /* source protocol addr */
188         uint8_t   ar_tha[ETH_ALEN]; /* dest hardware addr   */
189         uint32_t  ar_tip;           /* dest protocol addr   */
190 } __attribute__((packed));
191
192 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
193  * and initializes 'key' to match. */
194 void flow_extract(struct sk_buff *skb, uint16_t in_port,
195                   struct sw_flow_key *key)
196 {
197         struct ethhdr *mac;
198         struct udphdr *th;
199         int nh_ofs, th_ofs;
200
201         key->in_port = htons(in_port);
202         key->wildcards = 0;
203         memset(key->pad, '\0', sizeof(key->pad));
204
205         /* This code doesn't check that skb->len is long enough to contain the
206          * MAC or network header.  With a 46-byte minimum length frame this
207          * assumption is always correct. */
208
209         /* Doesn't verify checksums.  Should it? */
210
211         /* Data link layer.  We only support Ethernet. */
212         mac = eth_hdr(skb);
213         nh_ofs = sizeof(struct ethhdr);
214         if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
215                 /* This is an Ethernet II frame */
216                 key->dl_type = mac->h_proto;
217         } else {
218                 /* This is an 802.2 frame */
219                 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
220                         nh_ofs += sizeof(struct snap_hdr);
221                 } else {
222                         key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
223                         nh_ofs += sizeof(struct llc_pdu_un);
224                 }
225         }
226
227         /* Check for a VLAN tag */
228         if (likely(key->dl_type != __constant_htons(ETH_P_8021Q))) {
229                 key->dl_vlan = __constant_htons(OFP_VLAN_NONE);
230         } else {
231                 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
232                 key->dl_type = vh->h_vlan_encapsulated_proto;
233                 key->dl_vlan = vh->h_vlan_TCI & __constant_htons(VLAN_VID_MASK);
234                 nh_ofs += sizeof(*vh);
235         }
236         memcpy(key->dl_src, mac->h_source, ETH_ALEN);
237         memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
238         skb_set_network_header(skb, nh_ofs);
239
240         /* Network layer. */
241         if (likely(key->dl_type == htons(ETH_P_IP))) {
242                 struct iphdr *nh = ip_hdr(skb);
243                 key->nw_src = nh->saddr;
244                 key->nw_dst = nh->daddr;
245                 key->nw_proto = nh->protocol;
246                 th_ofs = nh_ofs + nh->ihl * 4;
247                 skb_set_transport_header(skb, th_ofs);
248
249                 /* Transport layer. */
250                 if ((key->nw_proto != IPPROTO_TCP && key->nw_proto != IPPROTO_UDP)
251                                 || skb->len < th_ofs + sizeof(struct udphdr)) {
252                         goto no_th;
253                 }
254                 th = udp_hdr(skb);
255                 key->tp_src = th->source;
256                 key->tp_dst = th->dest;
257
258                 return;
259         } else if (key->dl_type == htons(ETH_P_IPV6)) {
260                 struct ipv6hdr *nh = ipv6_hdr(skb);
261                 key->nw_src = hash_in6(&nh->saddr);
262                 key->nw_dst = hash_in6(&nh->daddr);
263                 /* FIXME: Need to traverse next-headers until we find the
264                  * upper-layer header. */
265                 key->nw_proto = 0;
266                 goto no_th;
267         } else if (key->dl_type == htons(ETH_P_ARP)) {
268                 /* just barely within 46-byte minimum packet */
269                 struct arp_eth_hdr *ah = (struct arp_eth_hdr *)skb_network_header(skb);
270                 if (ah->ar_hrd == htons(ARPHRD_ETHER)
271                     && ah->ar_pro == htons(ETH_P_IP)
272                     && ah->ar_hln == ETH_ALEN
273                     && ah->ar_pln == sizeof(key->nw_src))
274                 {
275                         /* check if sha/tha match dl_src/dl_dst? */
276                         key->nw_src = ah->ar_sip;
277                         key->nw_dst = ah->ar_tip;
278                         key->nw_proto = 0;
279                         goto no_th;
280                 }
281         } else {
282                 /* Fall through. */
283         }
284
285         key->nw_src = 0;
286         key->nw_dst = 0;
287         key->nw_proto = 0;
288
289 no_th:
290         key->tp_src = 0;
291         key->tp_dst = 0;
292 }
293
294 /* Initializes the flow module.
295  * Returns zero if successful or a negative error code. */
296 int flow_init(void)
297 {
298         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
299                                         0, NULL);
300         if (flow_cache == NULL)
301                 return -ENOMEM;
302
303         return 0;
304 }
305
306 /* Uninitializes the flow module. */
307 void flow_exit(void)
308 {
309         kmem_cache_destroy(flow_cache);
310 }
311