Add support for OFPFC_MODIFY Flow Mod command.
[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 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 deletion) 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_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
94 {
95     if (strict && d->wildcards != t->wildcards) {
96         return 0;
97     }
98     return flow_matches_1wild(t, d);
99 }
100
101 void
102 flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
103 {
104     to->wildcards = ntohl(from->wildcards) & OFPFW_ALL;
105     to->flow.reserved = 0;
106     to->flow.in_port = from->in_port;
107     to->flow.dl_vlan = from->dl_vlan;
108     memcpy(to->flow.dl_src, from->dl_src, ETH_ADDR_LEN);
109     memcpy(to->flow.dl_dst, from->dl_dst, ETH_ADDR_LEN);
110     to->flow.dl_type = from->dl_type;
111
112     to->flow.nw_src = to->flow.nw_dst = to->flow.nw_proto = 0;
113     to->flow.tp_src = to->flow.tp_dst = 0;
114
115 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
116 #define OFPFW_NW (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO)
117     if (to->wildcards & OFPFW_DL_TYPE) {
118         /* Can't sensibly match on network or transport headers if the
119          * data link type is unknown. */
120         to->wildcards |= OFPFW_NW | OFPFW_TP;
121     } else if (from->dl_type == htons(ETH_TYPE_IP)) {
122         to->flow.nw_src   = from->nw_src;
123         to->flow.nw_dst   = from->nw_dst;
124         to->flow.nw_proto = from->nw_proto;
125
126         if (to->wildcards & OFPFW_NW_PROTO) {
127             /* Can't sensibly match on transport headers if the network
128              * protocol is unknown. */
129             to->wildcards |= OFPFW_TP;
130         } else if (from->nw_proto == IPPROTO_TCP 
131                 || from->nw_proto == IPPROTO_UDP) {
132             to->flow.tp_src = from->tp_src;
133             to->flow.tp_dst = from->tp_dst;
134         } else {
135             /* Transport layer fields are undefined.  Mark them as
136              * exact-match to allow such flows to reside in table-hash,
137              * instead of falling into table-linear. */
138             to->wildcards &= ~OFPFW_TP;
139         }
140     } else {
141         /* Network and transport layer fields are undefined.  Mark them
142          * as exact-match to allow such flows to reside in table-hash,
143          * instead of falling into table-linear. */
144         to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
145     }
146
147         /* We set these late because code above adjusts to->wildcards. */
148         to->nw_src_mask = make_nw_mask(to->wildcards >> OFPFW_NW_SRC_SHIFT);
149         to->nw_dst_mask = make_nw_mask(to->wildcards >> OFPFW_NW_DST_SHIFT);
150 }
151
152 void
153 flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
154 {
155     to->wildcards = htonl(from->wildcards);
156     to->in_port   = from->flow.in_port;
157     to->dl_vlan   = from->flow.dl_vlan;
158     memcpy(to->dl_src, from->flow.dl_src, ETH_ADDR_LEN);
159     memcpy(to->dl_dst, from->flow.dl_dst, ETH_ADDR_LEN);
160     to->dl_type   = from->flow.dl_type;
161     to->nw_src        = from->flow.nw_src;
162     to->nw_dst        = from->flow.nw_dst;
163     to->nw_proto  = from->flow.nw_proto;
164     to->tp_src        = from->flow.tp_src;
165     to->tp_dst        = from->flow.tp_dst;
166     to->pad           = 0;
167 }
168
169 /* Allocates and returns a new flow with 'n_actions' action, using allocation
170  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
171 struct sw_flow *
172 flow_alloc(int n_actions)
173 {
174     struct sw_flow_actions *sfa;
175     struct sw_flow *flow = malloc(sizeof *flow);
176     if (!flow)
177         return NULL;
178
179     sfa = malloc(n_actions * sizeof sfa->actions[0]);
180     if (!sfa) {
181         free(flow);
182         return NULL;
183     }
184     sfa->n_actions = n_actions;
185     flow->sf_acts = sfa;
186     return flow;
187 }
188
189 /* Frees 'flow' immediately. */
190 void
191 flow_free(struct sw_flow *flow)
192 {
193     if (!flow) {
194         return; 
195     }
196     free(flow->sf_acts);
197     free(flow);
198 }
199
200 /* Copies 'actions' into a newly allocated structure for use by 'flow'
201  * and frees the structure that defined the previous actions. */
202 void flow_replace_acts(struct sw_flow *flow, const struct ofp_action *actions,
203         int n_actions)
204 {
205     struct sw_flow_actions *sfa;
206     int size = sizeof *sfa + (n_actions * sizeof sfa->actions[0]);
207
208     sfa = malloc(size);
209     if (unlikely(!sfa))
210         return;
211
212     sfa->n_actions = n_actions;
213     memcpy(sfa->actions, actions, n_actions * sizeof sfa->actions[0]);
214
215     free(flow->sf_acts);
216     flow->sf_acts = sfa;
217
218     return;
219 }
220
221 /* Prints a representation of 'key' to the kernel log. */
222 void
223 print_flow(const struct sw_flow_key *key)
224 {
225     const struct flow *f = &key->flow;
226     printf("wild%08x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
227            "->%02x:%02x:%02x:%02x:%02x:%02x "
228            "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
229            key->wildcards, ntohs(f->in_port), ntohs(f->dl_vlan),
230            f->dl_src[0], f->dl_src[1], f->dl_src[2],
231            f->dl_src[3], f->dl_src[4], f->dl_src[5],
232            f->dl_dst[0], f->dl_dst[1], f->dl_dst[2],
233            f->dl_dst[3], f->dl_dst[4], f->dl_dst[5],
234            ntohs(f->dl_type),
235            ((unsigned char *)&f->nw_src)[0],
236            ((unsigned char *)&f->nw_src)[1],
237            ((unsigned char *)&f->nw_src)[2],
238            ((unsigned char *)&f->nw_src)[3],
239            ((unsigned char *)&f->nw_dst)[0],
240            ((unsigned char *)&f->nw_dst)[1],
241            ((unsigned char *)&f->nw_dst)[2],
242            ((unsigned char *)&f->nw_dst)[3],
243            ntohs(f->tp_src), ntohs(f->tp_dst));
244 }
245
246 bool flow_timeout(struct sw_flow *flow)
247 {
248     time_t now = time_now();
249     if (flow->idle_timeout != OFP_FLOW_PERMANENT
250         && now > flow->used + flow->idle_timeout) {
251         flow->reason = OFPER_IDLE_TIMEOUT;
252         return true;
253     } else if (flow->hard_timeout != OFP_FLOW_PERMANENT
254                && now > flow->created + flow->hard_timeout) {
255         flow->reason = OFPER_HARD_TIMEOUT;
256         return true;
257     } else {
258         return false;
259     }
260 }
261
262 void flow_used(struct sw_flow *flow, struct buffer *buffer)
263 {
264     flow->used = time_now();
265     flow->packet_count++;
266     flow->byte_count += buffer->size;
267 }