Zero-out invalid fields when extracting a "match" and transforming it into a "flow".
[openvswitch] / 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         memset(to->pad, '\0', sizeof(to->pad));
71         to->in_port = from->in_port;
72         to->dl_vlan = from->dl_vlan;
73         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
74         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
75         to->dl_type = from->dl_type;
76
77         if (likely(from->dl_type == htons(ETH_P_IP))) {
78                 to->nw_src   = from->nw_src;
79                 to->nw_dst   = from->nw_dst;
80                 to->nw_proto = from->nw_proto;
81
82                 if ((from->nw_proto != IPPROTO_TCP && from->nw_proto != IPPROTO_UDP)) {
83                         goto no_th;
84                 }
85                 to->tp_src = from->tp_src;
86                 to->tp_dst = from->tp_dst;
87                 return;
88         } else if (from->dl_type == htons(ETH_P_ARP)) {
89                 to->nw_src = from->nw_src;
90                 to->nw_dst = from->nw_dst;
91                 to->nw_proto = 0;
92                 goto no_th;
93         }
94
95         to->nw_src = 0;
96         to->nw_dst = 0;
97         to->nw_proto = 0;
98
99 no_th:
100         to->tp_src = 0;
101         to->tp_dst = 0;
102 }
103
104 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
105 {
106         to->wildcards = htons(from->wildcards);
107         to->in_port   = from->in_port;
108         to->dl_vlan   = from->dl_vlan;
109         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
110         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
111         to->dl_type   = from->dl_type;
112         to->nw_src        = from->nw_src;
113         to->nw_dst        = from->nw_dst;
114         to->nw_proto  = from->nw_proto;
115         to->tp_src        = from->tp_src;
116         to->tp_dst        = from->tp_dst;
117         memset(to->pad, '\0', sizeof(to->pad));
118 }
119
120 /* Returns true if 'flow' can be deleted and set up for a deferred free, false
121  * if deletion has already been scheduled (by another thread).
122  *
123  * Caller must hold rcu_read_lock. */
124 int flow_del(struct sw_flow *flow)
125 {
126         return !atomic_cmpxchg(&flow->deleted, 0, 1);
127 }
128
129 /* Allocates and returns a new flow with 'n_actions' action, using allocation
130  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
131 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
132 {
133         struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
134         if (unlikely(!flow))
135                 return NULL;
136
137         flow->n_actions = n_actions;
138         flow->actions = kmalloc(n_actions * sizeof *flow->actions,
139                                 flags);
140         if (unlikely(!flow->actions) && n_actions > 0) {
141                 kmem_cache_free(flow_cache, flow);
142                 return NULL;
143         }
144         return flow;
145 }
146
147 /* Frees 'flow' immediately. */
148 void flow_free(struct sw_flow *flow)
149 {
150         if (unlikely(!flow))
151                 return;
152         kfree(flow->actions);
153         kmem_cache_free(flow_cache, flow);
154 }
155
156 /* RCU callback used by flow_deferred_free. */
157 static void rcu_callback(struct rcu_head *rcu)
158 {
159         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
160         flow_free(flow);
161 }
162
163 /* Schedules 'flow' to be freed after the next RCU grace period.
164  * The caller must hold rcu_read_lock for this to be sensible. */
165 void flow_deferred_free(struct sw_flow *flow)
166 {
167         call_rcu(&flow->rcu, rcu_callback);
168 }
169
170 /* Prints a representation of 'key' to the kernel log. */
171 void print_flow(const struct sw_flow_key *key)
172 {
173         printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
174                         "->%02x:%02x:%02x:%02x:%02x:%02x "
175                         "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
176                         key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
177                         key->dl_src[0], key->dl_src[1], key->dl_src[2],
178                         key->dl_src[3], key->dl_src[4], key->dl_src[5],
179                         key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
180                         key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
181                         ntohs(key->dl_type),
182                         ((unsigned char *)&key->nw_src)[0],
183                         ((unsigned char *)&key->nw_src)[1],
184                         ((unsigned char *)&key->nw_src)[2],
185                         ((unsigned char *)&key->nw_src)[3],
186                         ((unsigned char *)&key->nw_dst)[0],
187                         ((unsigned char *)&key->nw_dst)[1],
188                         ((unsigned char *)&key->nw_dst)[2],
189                         ((unsigned char *)&key->nw_dst)[3],
190                         ntohs(key->tp_src), ntohs(key->tp_dst));
191 }
192
193 uint32_t hash_in6(const struct in6_addr *in)
194 {
195         return (in->s6_addr32[0] ^ in->s6_addr32[1]
196                         ^ in->s6_addr32[2] ^ in->s6_addr32[3]);
197 }
198
199 // with inspiration from linux/if_arp.h
200 struct arp_eth_hdr {
201         uint16_t  ar_hrd;  /* format of hardware address    */
202         uint16_t  ar_pro;  /* format of protocol address    */
203         uint8_t   ar_hln;  /* length of hardware address    */
204         uint8_t   ar_pln;  /* length of protocol address    */
205         uint16_t  ar_op;   /* ARP opcode (command)          */
206
207         uint8_t   ar_sha[ETH_ALEN]; /* source hardware addr */
208         uint32_t  ar_sip;           /* source protocol addr */
209         uint8_t   ar_tha[ETH_ALEN]; /* dest hardware addr   */
210         uint32_t  ar_tip;           /* dest protocol addr   */
211 } __attribute__((packed));
212
213 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
214  * and initializes 'key' to match. */
215 void flow_extract(struct sk_buff *skb, uint16_t in_port,
216                   struct sw_flow_key *key)
217 {
218         struct ethhdr *mac;
219         struct udphdr *th;
220         int nh_ofs, th_ofs;
221
222         key->in_port = htons(in_port);
223         key->wildcards = 0;
224         memset(key->pad, '\0', sizeof(key->pad));
225
226         /* This code doesn't check that skb->len is long enough to contain the
227          * MAC or network header.  With a 46-byte minimum length frame this
228          * assumption is always correct. */
229
230         /* Doesn't verify checksums.  Should it? */
231
232         /* Data link layer.  We only support Ethernet. */
233         mac = eth_hdr(skb);
234         nh_ofs = sizeof(struct ethhdr);
235         if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
236                 /* This is an Ethernet II frame */
237                 key->dl_type = mac->h_proto;
238         } else {
239                 /* This is an 802.2 frame */
240                 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
241                         nh_ofs += sizeof(struct snap_hdr);
242                 } else {
243                         key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
244                         nh_ofs += sizeof(struct llc_pdu_un);
245                 }
246         }
247
248         /* Check for a VLAN tag */
249         if (likely(key->dl_type != htons(ETH_P_8021Q))) {
250                 key->dl_vlan = htons(OFP_VLAN_NONE);
251         } else {
252                 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
253                 key->dl_type = vh->h_vlan_encapsulated_proto;
254                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
255                 nh_ofs += sizeof(*vh);
256         }
257         memcpy(key->dl_src, mac->h_source, ETH_ALEN);
258         memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
259         skb_set_network_header(skb, nh_ofs);
260
261         /* Network layer. */
262         if (likely(key->dl_type == htons(ETH_P_IP))) {
263                 struct iphdr *nh = ip_hdr(skb);
264                 key->nw_src = nh->saddr;
265                 key->nw_dst = nh->daddr;
266                 key->nw_proto = nh->protocol;
267                 th_ofs = nh_ofs + nh->ihl * 4;
268                 skb_set_transport_header(skb, th_ofs);
269
270                 /* Transport layer. */
271                 if ((key->nw_proto != IPPROTO_TCP && key->nw_proto != IPPROTO_UDP)
272                                 || skb->len < th_ofs + sizeof(struct udphdr)) {
273                         goto no_th;
274                 }
275                 th = udp_hdr(skb);
276                 key->tp_src = th->source;
277                 key->tp_dst = th->dest;
278
279                 return;
280         } else if (key->dl_type == htons(ETH_P_IPV6)) {
281                 struct ipv6hdr *nh = ipv6_hdr(skb);
282                 key->nw_src = hash_in6(&nh->saddr);
283                 key->nw_dst = hash_in6(&nh->daddr);
284                 /* FIXME: Need to traverse next-headers until we find the
285                  * upper-layer header. */
286                 key->nw_proto = 0;
287                 goto no_th;
288         } else if (key->dl_type == htons(ETH_P_ARP)) {
289                 /* just barely within 46-byte minimum packet */
290                 struct arp_eth_hdr *ah = (struct arp_eth_hdr *)skb_network_header(skb);
291                 if (ah->ar_hrd == htons(ARPHRD_ETHER)
292                     && ah->ar_pro == htons(ETH_P_IP)
293                     && ah->ar_hln == ETH_ALEN
294                     && ah->ar_pln == sizeof(key->nw_src))
295                 {
296                         /* check if sha/tha match dl_src/dl_dst? */
297                         key->nw_src = ah->ar_sip;
298                         key->nw_dst = ah->ar_tip;
299                         key->nw_proto = 0;
300                         goto no_th;
301                 }
302         } else {
303                 /* Fall through. */
304         }
305
306         key->nw_src = 0;
307         key->nw_dst = 0;
308         key->nw_proto = 0;
309
310 no_th:
311         key->tp_src = 0;
312         key->tp_dst = 0;
313 }
314
315 /* Initializes the flow module.
316  * Returns zero if successful or a negative error code. */
317 int flow_init(void)
318 {
319         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
320                                         0, NULL);
321         if (flow_cache == NULL)
322                 return -ENOMEM;
323
324         return 0;
325 }
326
327 /* Uninitializes the flow module. */
328 void flow_exit(void)
329 {
330         kmem_cache_destroy(flow_cache);
331 }
332