Add-on hardware tables initial implementation.
[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 <net/llc_pdu.h>
13 #include <linux/ip.h>
14 #include <linux/kernel.h>
15 #include <linux/module.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 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
47 {
48         return flow_fields_match(a, b, (a->wildcards | b->wildcards));
49 }
50 EXPORT_SYMBOL(flow_matches);
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 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
58 {
59         if (strict && (t->wildcards != d->wildcards))
60                 return 0;
61
62         return flow_fields_match(t, d, d->wildcards);
63 }
64 EXPORT_SYMBOL(flow_del_matches);
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         }
88
89         to->nw_src = 0;
90         to->nw_dst = 0;
91         to->nw_proto = 0;
92
93 no_th:
94         to->tp_src = 0;
95         to->tp_dst = 0;
96 }
97
98 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
99 {
100         to->wildcards = htons(from->wildcards);
101         to->in_port   = from->in_port;
102         to->dl_vlan   = from->dl_vlan;
103         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
104         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
105         to->dl_type   = from->dl_type;
106         to->nw_src        = from->nw_src;
107         to->nw_dst        = from->nw_dst;
108         to->nw_proto  = from->nw_proto;
109         to->tp_src        = from->tp_src;
110         to->tp_dst        = from->tp_dst;
111         memset(to->pad, '\0', sizeof(to->pad));
112 }
113
114 /* Returns true if 'flow' can be deleted and set up for a deferred free, false
115  * if deletion has already been scheduled (by another thread).
116  *
117  * Caller must hold rcu_read_lock. */
118 int flow_del(struct sw_flow *flow)
119 {
120         return !atomic_cmpxchg(&flow->deleted, 0, 1);
121 }
122 EXPORT_SYMBOL(flow_del);
123
124 /* Allocates and returns a new flow with 'n_actions' action, using allocation
125  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
126 struct sw_flow *flow_alloc(int n_actions, gfp_t flags)
127 {
128         struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
129         if (unlikely(!flow))
130                 return NULL;
131
132         flow->n_actions = n_actions;
133         flow->actions = kmalloc(n_actions * sizeof *flow->actions,
134                                 flags);
135         if (unlikely(!flow->actions) && n_actions > 0) {
136                 kmem_cache_free(flow_cache, flow);
137                 return NULL;
138         }
139         return flow;
140 }
141
142 /* Frees 'flow' immediately. */
143 void flow_free(struct sw_flow *flow)
144 {
145         if (unlikely(!flow))
146                 return;
147         kfree(flow->actions);
148         kmem_cache_free(flow_cache, flow);
149 }
150 EXPORT_SYMBOL(flow_free);
151
152 /* RCU callback used by flow_deferred_free. */
153 static void rcu_callback(struct rcu_head *rcu)
154 {
155         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
156         flow_free(flow);
157 }
158
159 /* Schedules 'flow' to be freed after the next RCU grace period.
160  * The caller must hold rcu_read_lock for this to be sensible. */
161 void flow_deferred_free(struct sw_flow *flow)
162 {
163         call_rcu(&flow->rcu, rcu_callback);
164 }
165 EXPORT_SYMBOL(flow_deferred_free);
166
167 /* Prints a representation of 'key' to the kernel log. */
168 void print_flow(const struct sw_flow_key *key)
169 {
170         printk("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
171                         "->%02x:%02x:%02x:%02x:%02x:%02x "
172                         "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
173                         key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
174                         key->dl_src[0], key->dl_src[1], key->dl_src[2],
175                         key->dl_src[3], key->dl_src[4], key->dl_src[5],
176                         key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
177                         key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
178                         ntohs(key->dl_type),
179                         ((unsigned char *)&key->nw_src)[0],
180                         ((unsigned char *)&key->nw_src)[1],
181                         ((unsigned char *)&key->nw_src)[2],
182                         ((unsigned char *)&key->nw_src)[3],
183                         ((unsigned char *)&key->nw_dst)[0],
184                         ((unsigned char *)&key->nw_dst)[1],
185                         ((unsigned char *)&key->nw_dst)[2],
186                         ((unsigned char *)&key->nw_dst)[3],
187                         ntohs(key->tp_src), ntohs(key->tp_dst));
188 }
189 EXPORT_SYMBOL(print_flow);
190
191 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
192  * and initializes 'key' to match. */
193 void flow_extract(struct sk_buff *skb, uint16_t in_port,
194                   struct sw_flow_key *key)
195 {
196         struct ethhdr *mac;
197         struct udphdr *th;
198         int nh_ofs, th_ofs;
199
200         key->in_port = htons(in_port);
201         key->wildcards = 0;
202         memset(key->pad, '\0', sizeof(key->pad));
203
204         /* This code doesn't check that skb->len is long enough to contain the
205          * MAC or network header.  With a 46-byte minimum length frame this
206          * assumption is always correct. */
207
208         /* Doesn't verify checksums.  Should it? */
209
210         /* Data link layer.  We only support Ethernet. */
211         mac = eth_hdr(skb);
212         nh_ofs = sizeof(struct ethhdr);
213         if (likely(ntohs(mac->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF)) {
214                 /* This is an Ethernet II frame */
215                 key->dl_type = mac->h_proto;
216         } else {
217                 /* This is an 802.2 frame */
218                 if (snap_get_ethertype(skb, &key->dl_type) != -EINVAL) {
219                         nh_ofs += sizeof(struct snap_hdr);
220                 } else {
221                         key->dl_type = OFP_DL_TYPE_NOT_ETH_TYPE;
222                         nh_ofs += sizeof(struct llc_pdu_un);
223                 }
224         }
225
226         /* Check for a VLAN tag */
227         if (likely(key->dl_type != htons(ETH_P_8021Q))) {
228                 key->dl_vlan = htons(OFP_VLAN_NONE);
229         } else {
230                 struct vlan_hdr *vh = (struct vlan_hdr *)(skb_mac_header(skb) + nh_ofs);
231                 key->dl_type = vh->h_vlan_encapsulated_proto;
232                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
233                 nh_ofs += sizeof(*vh);
234         }
235         memcpy(key->dl_src, mac->h_source, ETH_ALEN);
236         memcpy(key->dl_dst, mac->h_dest, ETH_ALEN);
237         skb_set_network_header(skb, nh_ofs);
238
239         /* Network layer. */
240         if (likely(key->dl_type == htons(ETH_P_IP))) {
241                 struct iphdr *nh = ip_hdr(skb);
242                 key->nw_src = nh->saddr;
243                 key->nw_dst = nh->daddr;
244                 key->nw_proto = nh->protocol;
245                 th_ofs = nh_ofs + nh->ihl * 4;
246                 skb_set_transport_header(skb, th_ofs);
247
248                 /* Transport layer. */
249                 if ((key->nw_proto != IPPROTO_TCP && key->nw_proto != IPPROTO_UDP)
250                                 || skb->len < th_ofs + sizeof(struct udphdr)) {
251                         goto no_th;
252                 }
253                 th = udp_hdr(skb);
254                 key->tp_src = th->source;
255                 key->tp_dst = th->dest;
256
257                 return;
258         }
259
260         key->nw_src = 0;
261         key->nw_dst = 0;
262         key->nw_proto = 0;
263
264 no_th:
265         key->tp_src = 0;
266         key->tp_dst = 0;
267 }
268
269 /* Initializes the flow module.
270  * Returns zero if successful or a negative error code. */
271 int flow_init(void)
272 {
273         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
274                                         0, NULL);
275         if (flow_cache == NULL)
276                 return -ENOMEM;
277
278         return 0;
279 }
280
281 /* Uninitializes the flow module. */
282 void flow_exit(void)
283 {
284         kmem_cache_destroy(flow_cache);
285 }
286