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