Add userspace datapath to openflow.
[sliver-openvswitch.git] / switch / switch-flow.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "switch-flow.h"
36 #include <arpa/inet.h>
37 #include <assert.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "ofpbuf.h"
41 #include "openflow/openflow.h"
42 #include "packets.h"
43 #include "timeval.h"
44
45 /* Internal function used to compare fields in flow. */
46 static inline int
47 flow_fields_match(const struct flow *a, const struct flow *b, uint16_t w,
48                   uint32_t src_mask, uint32_t dst_mask)
49 {
50     return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
51             && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
52             && (w & OFPFW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
53             && (w & OFPFW_DL_DST || eth_addr_equals(a->dl_dst, b->dl_dst))
54             && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
55             && !((a->nw_src ^ b->nw_src) & src_mask)
56             && !((a->nw_dst ^ b->nw_dst) & dst_mask)
57             && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
58             && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
59             && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
60 }
61
62 static uint32_t make_nw_mask(int n_wild_bits)
63 {
64     n_wild_bits &= (1u << OFPFW_NW_SRC_BITS) - 1;
65     return n_wild_bits < 32 ? htonl(~((1u << n_wild_bits) - 1)) : 0;
66 }
67
68 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
69  * modulo wildcards in 'b', zero otherwise. */
70 inline int
71 flow_matches_1wild(const struct sw_flow_key *a, const struct sw_flow_key *b)
72 {
73     return flow_fields_match(&a->flow, &b->flow, b->wildcards,
74                              b->nw_src_mask, b->nw_dst_mask);
75 }
76
77 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
78  * modulo wildcards in 'a' or 'b', zero otherwise. */
79 inline int
80 flow_matches_2wild(const struct sw_flow_key *a, const struct sw_flow_key *b)
81 {
82     return flow_fields_match(&a->flow, &b->flow, a->wildcards | b->wildcards,
83                              a->nw_src_mask & b->nw_src_mask,
84                              a->nw_dst_mask & b->nw_dst_mask);
85 }
86
87 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key 
88  * describing the match) match, that is, if their fields are 
89  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
90  * wildcards must match in both 't_key' and 'd_key'.  Note that the
91  * table's wildcards are ignored unless 'strict' is set. */
92 int
93 flow_matches_desc(const struct sw_flow_key *t, const struct sw_flow_key *d, 
94         int strict)
95 {
96     if (strict && d->wildcards != t->wildcards) {
97         return 0;
98     }
99     return flow_matches_1wild(t, d);
100 }
101
102 void
103 flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
104 {
105     to->wildcards = ntohl(from->wildcards) & OFPFW_ALL;
106     to->flow.reserved = 0;
107     to->flow.in_port = from->in_port;
108     to->flow.dl_vlan = from->dl_vlan;
109     memcpy(to->flow.dl_src, from->dl_src, ETH_ADDR_LEN);
110     memcpy(to->flow.dl_dst, from->dl_dst, ETH_ADDR_LEN);
111     to->flow.dl_type = from->dl_type;
112
113     to->flow.nw_src = to->flow.nw_dst = to->flow.nw_proto = 0;
114     to->flow.tp_src = to->flow.tp_dst = 0;
115
116 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
117 #define OFPFW_NW (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO)
118     if (to->wildcards & OFPFW_DL_TYPE) {
119         /* Can't sensibly match on network or transport headers if the
120          * data link type is unknown. */
121         to->wildcards |= OFPFW_NW | OFPFW_TP;
122     } else if (from->dl_type == htons(ETH_TYPE_IP)) {
123         to->flow.nw_src   = from->nw_src;
124         to->flow.nw_dst   = from->nw_dst;
125         to->flow.nw_proto = from->nw_proto;
126
127         if (to->wildcards & OFPFW_NW_PROTO) {
128             /* Can't sensibly match on transport headers if the network
129              * protocol is unknown. */
130             to->wildcards |= OFPFW_TP;
131         } else if (from->nw_proto == IPPROTO_TCP 
132                 || from->nw_proto == IPPROTO_UDP
133                 || from->nw_proto == IPPROTO_ICMP) {
134             to->flow.tp_src = from->tp_src;
135             to->flow.tp_dst = from->tp_dst;
136         } else {
137             /* Transport layer fields are undefined.  Mark them as
138              * exact-match to allow such flows to reside in table-hash,
139              * instead of falling into table-linear. */
140             to->wildcards &= ~OFPFW_TP;
141         }
142     } else {
143         /* Network and transport layer fields are undefined.  Mark them
144          * as exact-match to allow such flows to reside in table-hash,
145          * instead of falling into table-linear. */
146         to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
147     }
148
149         /* We set these late because code above adjusts to->wildcards. */
150         to->nw_src_mask = make_nw_mask(to->wildcards >> OFPFW_NW_SRC_SHIFT);
151         to->nw_dst_mask = make_nw_mask(to->wildcards >> OFPFW_NW_DST_SHIFT);
152 }
153
154 void
155 flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
156 {
157     to->wildcards = htonl(from->wildcards);
158     to->in_port   = from->flow.in_port;
159     to->dl_vlan   = from->flow.dl_vlan;
160     memcpy(to->dl_src, from->flow.dl_src, ETH_ADDR_LEN);
161     memcpy(to->dl_dst, from->flow.dl_dst, ETH_ADDR_LEN);
162     to->dl_type   = from->flow.dl_type;
163     to->nw_src        = from->flow.nw_src;
164     to->nw_dst        = from->flow.nw_dst;
165     to->nw_proto  = from->flow.nw_proto;
166     to->tp_src        = from->flow.tp_src;
167     to->tp_dst        = from->flow.tp_dst;
168     to->pad           = 0;
169 }
170
171 /* Allocates and returns a new flow with room for 'actions_len' actions. 
172  * Returns the new flow or a null pointer on failure. */
173 struct sw_flow *
174 flow_alloc(size_t actions_len)
175 {
176     struct sw_flow_actions *sfa;
177     size_t size = sizeof *sfa + actions_len;
178     struct sw_flow *flow = malloc(sizeof *flow);
179     if (!flow)
180         return NULL;
181
182     sfa = malloc(size);
183     if (!sfa) {
184         free(flow);
185         return NULL;
186     }
187     sfa->actions_len = actions_len;
188     flow->sf_acts = sfa;
189     return flow;
190 }
191
192 /* Frees 'flow' immediately. */
193 void
194 flow_free(struct sw_flow *flow)
195 {
196     if (!flow) {
197         return; 
198     }
199     free(flow->sf_acts);
200     free(flow);
201 }
202
203 /* Copies 'actions' into a newly allocated structure for use by 'flow'
204  * and frees the structure that defined the previous actions. */
205 void flow_replace_acts(struct sw_flow *flow, 
206         const struct ofp_action_header *actions, size_t actions_len)
207 {
208     struct sw_flow_actions *sfa;
209     int size = sizeof *sfa + actions_len;
210
211     sfa = malloc(size);
212     if (unlikely(!sfa))
213         return;
214
215     sfa->actions_len = actions_len;
216     memcpy(sfa->actions, actions, actions_len);
217
218     free(flow->sf_acts);
219     flow->sf_acts = sfa;
220
221     return;
222 }
223
224 /* Prints a representation of 'key' to the kernel log. */
225 void
226 print_flow(const struct sw_flow_key *key)
227 {
228     const struct flow *f = &key->flow;
229     printf("wild%08x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
230            "->%02x:%02x:%02x:%02x:%02x:%02x "
231            "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
232            key->wildcards, ntohs(f->in_port), ntohs(f->dl_vlan),
233            f->dl_src[0], f->dl_src[1], f->dl_src[2],
234            f->dl_src[3], f->dl_src[4], f->dl_src[5],
235            f->dl_dst[0], f->dl_dst[1], f->dl_dst[2],
236            f->dl_dst[3], f->dl_dst[4], f->dl_dst[5],
237            ntohs(f->dl_type),
238            ((unsigned char *)&f->nw_src)[0],
239            ((unsigned char *)&f->nw_src)[1],
240            ((unsigned char *)&f->nw_src)[2],
241            ((unsigned char *)&f->nw_src)[3],
242            ((unsigned char *)&f->nw_dst)[0],
243            ((unsigned char *)&f->nw_dst)[1],
244            ((unsigned char *)&f->nw_dst)[2],
245            ((unsigned char *)&f->nw_dst)[3],
246            ntohs(f->tp_src), ntohs(f->tp_dst));
247 }
248
249 bool flow_timeout(struct sw_flow *flow)
250 {
251     time_t now = time_now();
252     if (flow->idle_timeout != OFP_FLOW_PERMANENT
253         && now > flow->used + flow->idle_timeout) {
254         flow->reason = OFPER_IDLE_TIMEOUT;
255         return true;
256     } else if (flow->hard_timeout != OFP_FLOW_PERMANENT
257                && now > flow->created + flow->hard_timeout) {
258         flow->reason = OFPER_HARD_TIMEOUT;
259         return true;
260     } else {
261         return false;
262     }
263 }
264
265 /* Returns nonzero if 'flow' contains an output action to 'out_port' or
266  * has the value OFPP_NONE. 'out_port' is in network-byte order. */
267 int flow_has_out_port(struct sw_flow *flow, uint16_t out_port)
268 {
269     struct sw_flow_actions *sf_acts = flow->sf_acts;
270     size_t actions_len = sf_acts->actions_len;
271     uint8_t *p = (uint8_t *)sf_acts->actions;
272
273     if (out_port == htons(OFPP_NONE))
274         return 1;
275
276     while (actions_len > 0) {
277         struct ofp_action_header *ah = (struct ofp_action_header *)p;
278         size_t len = ntohs(ah->len);
279
280         if (ah->type == htons(OFPAT_OUTPUT)) {
281             struct ofp_action_output *oa = (struct ofp_action_output *)p;
282             if (oa->port == out_port) {
283                 return 1;
284             }
285         }
286         p += len;
287         actions_len -= len;
288     }
289
290     return 0;
291 }
292
293 void flow_used(struct sw_flow *flow, struct ofpbuf *buffer)
294 {
295     flow->used = time_now();
296     flow->packet_count++;
297     flow->byte_count += buffer->size;
298 }