Add support for listing and deleting entries based on an output port.
[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/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/llc.h>
17 #include <linux/module.h>
18 #include <linux/tcp.h>
19 #include <linux/udp.h>
20 #include <linux/icmp.h>
21 #include <linux/in.h>
22 #include <linux/rcupdate.h>
23 #include <net/ip.h>
24
25 #include "openflow/openflow.h"
26 #include "compat.h"
27
28 struct kmem_cache *flow_cache;
29
30 /* Internal function used to compare fields in flow. */
31 static inline
32 int flow_fields_match(const struct sw_flow_key *a, const struct sw_flow_key *b,
33                       uint32_t w, uint32_t src_mask, uint32_t dst_mask)
34 {
35         return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
36                 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
37                 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ALEN))
38                 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ALEN))
39                 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
40                 && !((a->nw_src ^ b->nw_src) & src_mask)
41                 && !((a->nw_dst ^ b->nw_dst) & dst_mask)
42                 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
43                 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
44                 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
45 }
46
47 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
48  * modulo wildcards in 'b', zero otherwise. */
49 int flow_matches_1wild(const struct sw_flow_key *a,
50                        const struct sw_flow_key *b)
51 {
52         return flow_fields_match(a, b, b->wildcards,
53                                  b->nw_src_mask, b->nw_dst_mask);
54 }
55 EXPORT_SYMBOL(flow_matches_1wild);
56
57 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
58  * modulo wildcards in 'a' or 'b', zero otherwise. */
59 int flow_matches_2wild(const struct sw_flow_key *a,
60                        const struct sw_flow_key *b)
61 {
62         return flow_fields_match(a, b,
63                                  a->wildcards | b->wildcards,
64                                  a->nw_src_mask & b->nw_src_mask,
65                                  a->nw_dst_mask & b->nw_dst_mask);
66 }
67 EXPORT_SYMBOL(flow_matches_2wild);
68
69 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key
70  * describing the match) match, that is, if their fields are
71  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
72  * wildcards must match in both 't_key' and 'd_key'.  Note that the
73  * table's wildcards are ignored unless 'strict' is set. */
74 int flow_matches_desc(const struct sw_flow_key *t, const struct sw_flow_key *d, 
75                 int strict)
76 {
77         if (strict && d->wildcards != t->wildcards)
78                 return 0;
79         return flow_matches_1wild(t, d);
80 }
81 EXPORT_SYMBOL(flow_matches_desc);
82
83 static uint32_t make_nw_mask(int n_wild_bits)
84 {
85         n_wild_bits &= (1u << OFPFW_NW_SRC_BITS) - 1;
86         return n_wild_bits < 32 ? htonl(~((1u << n_wild_bits) - 1)) : 0;
87 }
88
89 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
90 {
91         to->wildcards = ntohl(from->wildcards) & OFPFW_ALL;
92         to->pad = 0;
93         to->in_port = from->in_port;
94         to->dl_vlan = from->dl_vlan;
95         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
96         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
97         to->dl_type = from->dl_type;
98
99         to->nw_src = to->nw_dst = to->nw_proto = 0;
100         to->tp_src = to->tp_dst = 0;
101
102 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
103 #define OFPFW_NW (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO)
104         if (to->wildcards & OFPFW_DL_TYPE) {
105                 /* Can't sensibly match on network or transport headers if the
106                  * data link type is unknown. */
107                 to->wildcards |= OFPFW_NW | OFPFW_TP;
108         } else if (from->dl_type == htons(ETH_P_IP)) {
109                 to->nw_src   = from->nw_src;
110                 to->nw_dst   = from->nw_dst;
111                 to->nw_proto = from->nw_proto;
112
113                 if (to->wildcards & OFPFW_NW_PROTO) {
114                         /* Can't sensibly match on transport headers if the
115                          * network protocol is unknown. */
116                         to->wildcards |= OFPFW_TP;
117                 } else if (from->nw_proto == IPPROTO_TCP
118                                 || from->nw_proto == IPPROTO_UDP
119                                 || from->nw_proto == IPPROTO_ICMP) {
120                         to->tp_src = from->tp_src;
121                         to->tp_dst = from->tp_dst;
122                 } else {
123                         /* Transport layer fields are undefined.  Mark them as
124                          * exact-match to allow such flows to reside in
125                          * table-hash, instead of falling into table-linear. */
126                         to->wildcards &= ~OFPFW_TP;
127                 }
128         } else {
129                 /* Network and transport layer fields are undefined.  Mark them
130                  * as exact-match to allow such flows to reside in table-hash,
131                  * instead of falling into table-linear. */
132                 to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
133         }
134
135         /* We set these late because code above adjusts to->wildcards. */
136         to->nw_src_mask = make_nw_mask(to->wildcards >> OFPFW_NW_SRC_SHIFT);
137         to->nw_dst_mask = make_nw_mask(to->wildcards >> OFPFW_NW_DST_SHIFT);
138 }
139
140 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
141 {
142         to->wildcards = htonl(from->wildcards);
143         to->in_port   = from->in_port;
144         to->dl_vlan   = from->dl_vlan;
145         memcpy(to->dl_src, from->dl_src, ETH_ALEN);
146         memcpy(to->dl_dst, from->dl_dst, ETH_ALEN);
147         to->dl_type   = from->dl_type;
148         to->nw_src    = from->nw_src;
149         to->nw_dst    = from->nw_dst;
150         to->nw_proto  = from->nw_proto;
151         to->tp_src    = from->tp_src;
152         to->tp_dst    = from->tp_dst;
153         to->pad       = 0;
154 }
155
156 int flow_timeout(struct sw_flow *flow)
157 {
158         if (flow->idle_timeout != OFP_FLOW_PERMANENT
159             && time_after(jiffies, flow->used + flow->idle_timeout * HZ))
160                 return OFPER_IDLE_TIMEOUT;
161         else if (flow->hard_timeout != OFP_FLOW_PERMANENT
162                  && time_after(jiffies,
163                                flow->init_time + flow->hard_timeout * HZ))
164                 return OFPER_HARD_TIMEOUT;
165         else
166                 return -1;
167 }
168 EXPORT_SYMBOL(flow_timeout);
169
170 /* Returns nonzero if 'flow' contains an output action to 'out_port' or
171  * has the value OFPP_NONE. 'out_port' is in network-byte order. */
172 int flow_has_out_port(struct sw_flow *flow, uint16_t out_port)
173 {
174         struct sw_flow_actions *sf_acts;
175         size_t actions_len;
176         uint8_t *p;
177
178         if (out_port == htons(OFPP_NONE))
179                 return 1;
180
181         sf_acts = rcu_dereference(flow->sf_acts);
182
183         actions_len = sf_acts->actions_len;
184         p = (uint8_t *)sf_acts->actions;
185
186         while (actions_len > 0) {
187                 struct ofp_action_header *ah = (struct ofp_action_header *)p;
188                 size_t len = ntohs(ah->len);
189
190                 if (ah->type == htons(OFPAT_OUTPUT)) {
191                         struct ofp_action_output *oa = (struct ofp_action_output *)p;
192                         if (oa->port == out_port)
193                                 return 1;
194                 }
195
196                 p += len;
197                 actions_len -= len;
198         }
199
200         return 0;
201 }
202 EXPORT_SYMBOL(flow_has_out_port);
203
204 /* Allocates and returns a new flow with room for 'actions_len' actions, 
205  * using allocation flags 'flags'.  Returns the new flow or a null pointer 
206  * on failure. */
207 struct sw_flow *flow_alloc(size_t actions_len, gfp_t flags)
208 {
209         struct sw_flow_actions *sfa;
210         size_t size = sizeof *sfa + actions_len;
211         struct sw_flow *flow = kmem_cache_alloc(flow_cache, flags);
212         if (unlikely(!flow))
213                 return NULL;
214
215         sfa = kmalloc(size, flags);
216         if (unlikely(!sfa)) {
217                 kmem_cache_free(flow_cache, flow);
218                 return NULL;
219         }
220         sfa->actions_len = actions_len;
221         flow->sf_acts = sfa;
222
223         return flow;
224 }
225
226 /* Frees 'flow' immediately. */
227 void flow_free(struct sw_flow *flow)
228 {
229         if (unlikely(!flow))
230                 return;
231         kfree(flow->sf_acts);
232         kmem_cache_free(flow_cache, flow);
233 }
234 EXPORT_SYMBOL(flow_free);
235
236 /* RCU callback used by flow_deferred_free. */
237 static void rcu_free_flow_callback(struct rcu_head *rcu)
238 {
239         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
240         flow_free(flow);
241 }
242
243 /* Schedules 'flow' to be freed after the next RCU grace period.
244  * The caller must hold rcu_read_lock for this to be sensible. */
245 void flow_deferred_free(struct sw_flow *flow)
246 {
247         call_rcu(&flow->rcu, rcu_free_flow_callback);
248 }
249 EXPORT_SYMBOL(flow_deferred_free);
250
251 /* RCU callback used by flow_deferred_free_acts. */
252 static void rcu_free_acts_callback(struct rcu_head *rcu)
253 {
254         struct sw_flow_actions *sf_acts = container_of(rcu, 
255                         struct sw_flow_actions, rcu);
256         kfree(sf_acts);
257 }
258
259 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
260  * The caller must hold rcu_read_lock for this to be sensible. */
261 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
262 {
263         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
264 }
265 EXPORT_SYMBOL(flow_deferred_free_acts);
266
267 /* Copies 'actions' into a newly allocated structure for use by 'flow'
268  * and safely frees the structure that defined the previous actions. */
269 void flow_replace_acts(struct sw_flow *flow, 
270                 const struct ofp_action_header *actions, size_t actions_len)
271 {
272         struct sw_flow_actions *sfa;
273         struct sw_flow_actions *orig_sfa = flow->sf_acts;
274         size_t size = sizeof *sfa + actions_len;
275
276         sfa = kmalloc(size, GFP_ATOMIC);
277         if (unlikely(!sfa))
278                 return;
279
280         sfa->actions_len = actions_len;
281         memcpy(sfa->actions, actions, actions_len);
282
283         rcu_assign_pointer(flow->sf_acts, sfa);
284         flow_deferred_free_acts(orig_sfa);
285
286         return;
287 }
288 EXPORT_SYMBOL(flow_replace_acts);
289
290 /* Prints a representation of 'key' to the kernel log. */
291 void print_flow(const struct sw_flow_key *key)
292 {
293         printk("wild%08x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
294                         "->%02x:%02x:%02x:%02x:%02x:%02x "
295                         "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
296                         key->wildcards, ntohs(key->in_port), ntohs(key->dl_vlan),
297                         key->dl_src[0], key->dl_src[1], key->dl_src[2],
298                         key->dl_src[3], key->dl_src[4], key->dl_src[5],
299                         key->dl_dst[0], key->dl_dst[1], key->dl_dst[2],
300                         key->dl_dst[3], key->dl_dst[4], key->dl_dst[5],
301                         ntohs(key->dl_type),
302                         ((unsigned char *)&key->nw_src)[0],
303                         ((unsigned char *)&key->nw_src)[1],
304                         ((unsigned char *)&key->nw_src)[2],
305                         ((unsigned char *)&key->nw_src)[3],
306                         ((unsigned char *)&key->nw_dst)[0],
307                         ((unsigned char *)&key->nw_dst)[1],
308                         ((unsigned char *)&key->nw_dst)[2],
309                         ((unsigned char *)&key->nw_dst)[3],
310                         ntohs(key->tp_src), ntohs(key->tp_dst));
311 }
312 EXPORT_SYMBOL(print_flow);
313
314 #define SNAP_OUI_LEN 3
315
316 struct eth_snap_hdr
317 {
318         struct ethhdr eth;
319         uint8_t  dsap;  /* Always 0xAA */
320         uint8_t  ssap;  /* Always 0xAA */
321         uint8_t  ctrl;
322         uint8_t  oui[SNAP_OUI_LEN];
323         uint16_t ethertype;
324 } __attribute__ ((packed));
325
326 static int is_snap(const struct eth_snap_hdr *esh)
327 {
328         return (esh->dsap == LLC_SAP_SNAP
329                 && esh->ssap == LLC_SAP_SNAP
330                 && !memcmp(esh->oui, "\0\0\0", 3));
331 }
332
333 static int iphdr_ok(struct sk_buff *skb)
334 {
335         int nh_ofs = skb_network_offset(skb);
336         if (skb->len >= nh_ofs + sizeof(struct iphdr)) {
337                 int ip_len = ip_hdrlen(skb);
338                 return (ip_len >= sizeof(struct iphdr)
339                         && pskb_may_pull(skb, nh_ofs + ip_len));
340         }
341         return 0;
342 }
343
344 static int tcphdr_ok(struct sk_buff *skb)
345 {
346         int th_ofs = skb_transport_offset(skb);
347         if (pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))) {
348                 int tcp_len = tcp_hdrlen(skb);
349                 return (tcp_len >= sizeof(struct tcphdr)
350                         && skb->len >= th_ofs + tcp_len);
351         }
352         return 0;
353 }
354
355 static int udphdr_ok(struct sk_buff *skb)
356 {
357         int th_ofs = skb_transport_offset(skb);
358         return pskb_may_pull(skb, th_ofs + sizeof(struct udphdr));
359 }
360
361 static int icmphdr_ok(struct sk_buff *skb)
362 {
363         int th_ofs = skb_transport_offset(skb);
364         return pskb_may_pull(skb, th_ofs + sizeof(struct icmphdr));
365 }
366
367 /* Parses the Ethernet frame in 'skb', which was received on 'in_port',
368  * and initializes 'key' to match.  Returns 1 if 'skb' contains an IP
369  * fragment, 0 otherwise. */
370 int flow_extract(struct sk_buff *skb, uint16_t in_port,
371                  struct sw_flow_key *key)
372 {
373         struct ethhdr *eth;
374         struct eth_snap_hdr *esh;
375         int retval = 0;
376         int nh_ofs;
377
378         memset(key, 0, sizeof *key);
379         key->dl_vlan = htons(OFP_VLAN_NONE);
380         key->in_port = htons(in_port);
381
382         if (skb->len < sizeof *eth)
383                 return 0;
384         if (!pskb_may_pull(skb, skb->len >= 64 ? 64 : skb->len)) {
385                 return 0;
386         }
387
388         skb_reset_mac_header(skb);
389         eth = eth_hdr(skb);
390         esh = (struct eth_snap_hdr *) eth;
391         nh_ofs = sizeof *eth;
392         if (likely(ntohs(eth->h_proto) >= OFP_DL_TYPE_ETH2_CUTOFF))
393                 key->dl_type = eth->h_proto;
394         else if (skb->len >= sizeof *esh && is_snap(esh)) {
395                 key->dl_type = esh->ethertype;
396                 nh_ofs = sizeof *esh;
397         } else {
398                 key->dl_type = htons(OFP_DL_TYPE_NOT_ETH_TYPE);
399                 if (skb->len >= nh_ofs + sizeof(struct llc_pdu_un)) {
400                         nh_ofs += sizeof(struct llc_pdu_un); 
401                 }
402         }
403
404         /* Check for a VLAN tag */
405         if (key->dl_type == htons(ETH_P_8021Q) &&
406             skb->len >= nh_ofs + sizeof(struct vlan_hdr)) {
407                 struct vlan_hdr *vh = (struct vlan_hdr*)(skb->data + nh_ofs);
408                 key->dl_type = vh->h_vlan_encapsulated_proto;
409                 key->dl_vlan = vh->h_vlan_TCI & htons(VLAN_VID_MASK);
410                 nh_ofs += sizeof(struct vlan_hdr);
411         }
412         memcpy(key->dl_src, eth->h_source, ETH_ALEN);
413         memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
414         skb_set_network_header(skb, nh_ofs);
415
416         /* Network layer. */
417         if (key->dl_type == htons(ETH_P_IP) && iphdr_ok(skb)) {
418                 struct iphdr *nh = ip_hdr(skb);
419                 int th_ofs = nh_ofs + nh->ihl * 4;
420                 key->nw_src = nh->saddr;
421                 key->nw_dst = nh->daddr;
422                 key->nw_proto = nh->protocol;
423                 skb_set_transport_header(skb, th_ofs);
424
425                 /* Transport layer. */
426                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET))) {
427                         if (key->nw_proto == IPPROTO_TCP) {
428                                 if (tcphdr_ok(skb)) {
429                                         struct tcphdr *tcp = tcp_hdr(skb);
430                                         key->tp_src = tcp->source;
431                                         key->tp_dst = tcp->dest;
432                                 } else {
433                                         /* Avoid tricking other code into
434                                          * thinking that this packet has an L4
435                                          * header. */
436                                         key->nw_proto = 0;
437                                 }
438                         } else if (key->nw_proto == IPPROTO_UDP) {
439                                 if (udphdr_ok(skb)) {
440                                         struct udphdr *udp = udp_hdr(skb);
441                                         key->tp_src = udp->source;
442                                         key->tp_dst = udp->dest;
443                                 } else {
444                                         /* Avoid tricking other code into
445                                          * thinking that this packet has an L4
446                                          * header. */
447                                         key->nw_proto = 0;
448                                 }
449                         } else if (key->nw_proto == IPPROTO_ICMP) {
450                                 if (icmphdr_ok(skb)) {
451                                         struct icmphdr *icmp = icmp_hdr(skb);
452                                         /* The ICMP type and code fields use the 16-bit
453                                          * transport port fields, so we need to store them
454                                          * in 16-bit network byte order. */
455                                         key->icmp_type = htons(icmp->type);
456                                         key->icmp_code = htons(icmp->code);
457                                 } else {
458                                         /* Avoid tricking other code into
459                                          * thinking that this packet has an L4
460                                          * header. */
461                                         key->nw_proto = 0;
462                                 }
463                         }
464                 } else {
465                         retval = 1;
466                 }
467         } else {
468                 skb_reset_transport_header(skb);
469         }
470         return retval;
471 }
472
473 /* Initializes the flow module.
474  * Returns zero if successful or a negative error code. */
475 int flow_init(void)
476 {
477         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
478                                         0, NULL);
479         if (flow_cache == NULL)
480                 return -ENOMEM;
481
482         return 0;
483 }
484
485 /* Uninitializes the flow module. */
486 void flow_exit(void)
487 {
488         kmem_cache_destroy(flow_cache);
489 }
490