Use signal-based timekeeping functions throughout the source base.
[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 "buffer.h"
41 #include "openflow.h"
42 #include "packets.h"
43 #include "timeval.h"
44
45 /* Internal function used to compare fields in flow. */
46 static inline
47 int flow_fields_match(const struct flow *a, const struct flow *b, uint16_t w)
48 {
49     return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
50             && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
51             && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ADDR_LEN))
52             && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ADDR_LEN))
53             && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
54             && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
55             && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
56             && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
57             && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
58             && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
59 }
60
61 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
62  * modulo wildcards, zero otherwise. */
63 inline
64 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
65 {
66     return flow_fields_match(&a->flow, &b->flow, a->wildcards | b->wildcards);
67 }
68
69 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key 
70  * describing the deletion) 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 inline
75 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
76 {
77     if (strict && t->wildcards != d->wildcards)
78         return 0;
79
80     return flow_fields_match(&t->flow, &d->flow, d->wildcards);
81 }
82
83 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
84 {
85     to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
86     to->flow.reserved = 0;
87     to->flow.in_port = from->in_port;
88     to->flow.dl_vlan = from->dl_vlan;
89     memcpy(to->flow.dl_src, from->dl_src, ETH_ADDR_LEN);
90     memcpy(to->flow.dl_dst, from->dl_dst, ETH_ADDR_LEN);
91     to->flow.dl_type = from->dl_type;
92
93     to->flow.nw_src = to->flow.nw_dst = to->flow.nw_proto = 0;
94     to->flow.tp_src = to->flow.tp_dst = 0;
95
96 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
97 #define OFPFW_NW (OFPFW_NW_SRC | OFPFW_NW_DST | OFPFW_NW_PROTO)
98     if (to->wildcards & OFPFW_DL_TYPE) {
99         /* Can't sensibly match on network or transport headers if the
100          * data link type is unknown. */
101         to->wildcards |= OFPFW_NW | OFPFW_TP;
102     } else if (from->dl_type == htons(ETH_TYPE_IP)) {
103         to->flow.nw_src   = from->nw_src;
104         to->flow.nw_dst   = from->nw_dst;
105         to->flow.nw_proto = from->nw_proto;
106
107         if (to->wildcards & OFPFW_NW_PROTO) {
108             /* Can't sensibly match on transport headers if the network
109              * protocol is unknown. */
110             to->wildcards |= OFPFW_TP;
111         } else if (from->nw_proto == IPPROTO_TCP 
112                 || from->nw_proto == IPPROTO_UDP) {
113             to->flow.tp_src = from->tp_src;
114             to->flow.tp_dst = from->tp_dst;
115         } else {
116             /* Transport layer fields are undefined.  Mark them as
117              * exact-match to allow such flows to reside in table-hash,
118              * instead of falling into table-linear. */
119             to->wildcards &= ~OFPFW_TP;
120         }
121     } else {
122         /* Network and transport layer fields are undefined.  Mark them
123          * as exact-match to allow such flows to reside in table-hash,
124          * instead of falling into table-linear. */
125         to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
126     }
127 }
128
129 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
130 {
131     to->wildcards = htons(from->wildcards);
132     to->in_port   = from->flow.in_port;
133     to->dl_vlan   = from->flow.dl_vlan;
134     memcpy(to->dl_src, from->flow.dl_src, ETH_ADDR_LEN);
135     memcpy(to->dl_dst, from->flow.dl_dst, ETH_ADDR_LEN);
136     to->dl_type   = from->flow.dl_type;
137     to->nw_src        = from->flow.nw_src;
138     to->nw_dst        = from->flow.nw_dst;
139     to->nw_proto  = from->flow.nw_proto;
140     to->tp_src        = from->flow.tp_src;
141     to->tp_dst        = from->flow.tp_dst;
142     memset(to->pad, '\0', sizeof(to->pad));
143 }
144
145 /* Allocates and returns a new flow with 'n_actions' action, using allocation
146  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
147 struct sw_flow *flow_alloc(int n_actions)
148 {
149     struct sw_flow *flow = malloc(sizeof *flow);
150     if (!flow)
151         return NULL;
152
153     flow->n_actions = n_actions;
154     flow->actions = malloc(n_actions * sizeof *flow->actions);
155     if (!flow->actions && n_actions > 0) {
156         free(flow);
157         return NULL;
158     }
159     return flow;
160 }
161
162 /* Frees 'flow' immediately. */
163 void flow_free(struct sw_flow *flow)
164 {
165     if (!flow) {
166         return; 
167     }
168     free(flow->actions);
169     free(flow);
170 }
171
172 /* Prints a representation of 'key' to the kernel log. */
173 void print_flow(const struct sw_flow_key *key)
174 {
175     const struct flow *f = &key->flow;
176     printf("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
177            "->%02x:%02x:%02x:%02x:%02x:%02x "
178            "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
179            key->wildcards, ntohs(f->in_port), ntohs(f->dl_vlan),
180            f->dl_src[0], f->dl_src[1], f->dl_src[2],
181            f->dl_src[3], f->dl_src[4], f->dl_src[5],
182            f->dl_dst[0], f->dl_dst[1], f->dl_dst[2],
183            f->dl_dst[3], f->dl_dst[4], f->dl_dst[5],
184            ntohs(f->dl_type),
185            ((unsigned char *)&f->nw_src)[0],
186            ((unsigned char *)&f->nw_src)[1],
187            ((unsigned char *)&f->nw_src)[2],
188            ((unsigned char *)&f->nw_src)[3],
189            ((unsigned char *)&f->nw_dst)[0],
190            ((unsigned char *)&f->nw_dst)[1],
191            ((unsigned char *)&f->nw_dst)[2],
192            ((unsigned char *)&f->nw_dst)[3],
193            ntohs(f->tp_src), ntohs(f->tp_dst));
194 }
195
196 bool flow_timeout(struct sw_flow *flow)
197 {
198     time_t now = time_now();
199     if (flow->idle_timeout != OFP_FLOW_PERMANENT
200         && now > flow->used + flow->idle_timeout) {
201         flow->reason = OFPER_IDLE_TIMEOUT;
202         return true;
203     } else if (flow->hard_timeout != OFP_FLOW_PERMANENT
204                && now > flow->created + flow->hard_timeout) {
205         flow->reason = OFPER_HARD_TIMEOUT;
206         return true;
207     } else {
208         return false;
209     }
210 }
211
212 void flow_used(struct sw_flow *flow, struct buffer *buffer)
213 {
214     flow->used = time_now();
215     flow->packet_count++;
216     flow->byte_count += buffer->size;
217 }