1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
35 #include "switch-flow.h"
36 #include <arpa/inet.h>
45 /* Internal function used to compare fields in flow. */
47 flow_fields_match(const struct flow *a, const struct flow *b, uint16_t w,
48 uint32_t src_mask, uint32_t dst_mask)
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));
62 static uint32_t make_nw_mask(int n_wild_bits)
64 n_wild_bits &= (1u << OFPFW_NW_SRC_BITS) - 1;
65 return n_wild_bits < 32 ? htonl(~((1u << n_wild_bits) - 1)) : 0;
68 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
69 * modulo wildcards in 'b', zero otherwise. */
71 flow_matches_1wild(const struct sw_flow_key *a, const struct sw_flow_key *b)
73 return flow_fields_match(&a->flow, &b->flow, b->wildcards,
74 b->nw_src_mask, b->nw_dst_mask);
77 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
78 * modulo wildcards in 'a' or 'b', zero otherwise. */
80 flow_matches_2wild(const struct sw_flow_key *a, const struct sw_flow_key *b)
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);
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. */
93 flow_matches_desc(const struct sw_flow_key *t, const struct sw_flow_key *d,
96 if (strict && d->wildcards != t->wildcards) {
99 return flow_matches_1wild(t, d);
103 flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
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;
113 to->flow.nw_src = to->flow.nw_dst = to->flow.nw_proto = 0;
114 to->flow.tp_src = to->flow.tp_dst = 0;
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;
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 to->flow.tp_src = from->tp_src;
134 to->flow.tp_dst = from->tp_dst;
136 /* Transport layer fields are undefined. Mark them as
137 * exact-match to allow such flows to reside in table-hash,
138 * instead of falling into table-linear. */
139 to->wildcards &= ~OFPFW_TP;
142 /* Network and transport layer fields are undefined. Mark them
143 * as exact-match to allow such flows to reside in table-hash,
144 * instead of falling into table-linear. */
145 to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
148 /* We set these late because code above adjusts to->wildcards. */
149 to->nw_src_mask = make_nw_mask(to->wildcards >> OFPFW_NW_SRC_SHIFT);
150 to->nw_dst_mask = make_nw_mask(to->wildcards >> OFPFW_NW_DST_SHIFT);
154 flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
156 to->wildcards = htonl(from->wildcards);
157 to->in_port = from->flow.in_port;
158 to->dl_vlan = from->flow.dl_vlan;
159 memcpy(to->dl_src, from->flow.dl_src, ETH_ADDR_LEN);
160 memcpy(to->dl_dst, from->flow.dl_dst, ETH_ADDR_LEN);
161 to->dl_type = from->flow.dl_type;
162 to->nw_src = from->flow.nw_src;
163 to->nw_dst = from->flow.nw_dst;
164 to->nw_proto = from->flow.nw_proto;
165 to->tp_src = from->flow.tp_src;
166 to->tp_dst = from->flow.tp_dst;
170 /* Allocates and returns a new flow with 'n_actions' action, using allocation
171 * flags 'flags'. Returns the new flow or a null pointer on failure. */
173 flow_alloc(int n_actions)
175 struct sw_flow_actions *sfa;
176 struct sw_flow *flow = malloc(sizeof *flow);
180 sfa = malloc(n_actions * sizeof sfa->actions[0]);
185 sfa->n_actions = n_actions;
190 /* Frees 'flow' immediately. */
192 flow_free(struct sw_flow *flow)
201 /* Copies 'actions' into a newly allocated structure for use by 'flow'
202 * and frees the structure that defined the previous actions. */
203 void flow_replace_acts(struct sw_flow *flow, const struct ofp_action *actions,
206 struct sw_flow_actions *sfa;
207 int size = sizeof *sfa + (n_actions * sizeof sfa->actions[0]);
213 sfa->n_actions = n_actions;
214 memcpy(sfa->actions, actions, n_actions * sizeof sfa->actions[0]);
222 /* Prints a representation of 'key' to the kernel log. */
224 print_flow(const struct sw_flow_key *key)
226 const struct flow *f = &key->flow;
227 printf("wild%08x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
228 "->%02x:%02x:%02x:%02x:%02x:%02x "
229 "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
230 key->wildcards, ntohs(f->in_port), ntohs(f->dl_vlan),
231 f->dl_src[0], f->dl_src[1], f->dl_src[2],
232 f->dl_src[3], f->dl_src[4], f->dl_src[5],
233 f->dl_dst[0], f->dl_dst[1], f->dl_dst[2],
234 f->dl_dst[3], f->dl_dst[4], f->dl_dst[5],
236 ((unsigned char *)&f->nw_src)[0],
237 ((unsigned char *)&f->nw_src)[1],
238 ((unsigned char *)&f->nw_src)[2],
239 ((unsigned char *)&f->nw_src)[3],
240 ((unsigned char *)&f->nw_dst)[0],
241 ((unsigned char *)&f->nw_dst)[1],
242 ((unsigned char *)&f->nw_dst)[2],
243 ((unsigned char *)&f->nw_dst)[3],
244 ntohs(f->tp_src), ntohs(f->tp_dst));
247 bool flow_timeout(struct sw_flow *flow)
249 time_t now = time_now();
250 if (flow->idle_timeout != OFP_FLOW_PERMANENT
251 && now > flow->used + flow->idle_timeout) {
252 flow->reason = OFPER_IDLE_TIMEOUT;
254 } else if (flow->hard_timeout != OFP_FLOW_PERMANENT
255 && now > flow->created + flow->hard_timeout) {
256 flow->reason = OFPER_HARD_TIMEOUT;
263 void flow_used(struct sw_flow *flow, struct ofpbuf *buffer)
265 flow->used = time_now();
266 flow->packet_count++;
267 flow->byte_count += buffer->size;