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 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. */
93 flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
95 if (strict && d->wildcards != t->wildcards) {
98 return flow_matches_1wild(t, d);
102 flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
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;
112 to->flow.nw_src = to->flow.nw_dst = to->flow.nw_proto = 0;
113 to->flow.tp_src = to->flow.tp_dst = 0;
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;
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;
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;
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);
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);
153 flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
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;
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. */
172 flow_alloc(int n_actions)
174 struct sw_flow *flow = malloc(sizeof *flow);
178 flow->n_actions = n_actions;
179 flow->actions = malloc(n_actions * sizeof *flow->actions);
180 if (!flow->actions && n_actions > 0) {
187 /* Frees 'flow' immediately. */
189 flow_free(struct sw_flow *flow)
200 /* Prints a representation of 'key' to the kernel log. */
202 print_flow(const struct sw_flow_key *key)
204 const struct flow *f = &key->flow;
205 printf("wild%08x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
206 "->%02x:%02x:%02x:%02x:%02x:%02x "
207 "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
208 key->wildcards, ntohs(f->in_port), ntohs(f->dl_vlan),
209 f->dl_src[0], f->dl_src[1], f->dl_src[2],
210 f->dl_src[3], f->dl_src[4], f->dl_src[5],
211 f->dl_dst[0], f->dl_dst[1], f->dl_dst[2],
212 f->dl_dst[3], f->dl_dst[4], f->dl_dst[5],
214 ((unsigned char *)&f->nw_src)[0],
215 ((unsigned char *)&f->nw_src)[1],
216 ((unsigned char *)&f->nw_src)[2],
217 ((unsigned char *)&f->nw_src)[3],
218 ((unsigned char *)&f->nw_dst)[0],
219 ((unsigned char *)&f->nw_dst)[1],
220 ((unsigned char *)&f->nw_dst)[2],
221 ((unsigned char *)&f->nw_dst)[3],
222 ntohs(f->tp_src), ntohs(f->tp_dst));
225 bool flow_timeout(struct sw_flow *flow)
227 time_t now = time_now();
228 if (flow->idle_timeout != OFP_FLOW_PERMANENT
229 && now > flow->used + flow->idle_timeout) {
230 flow->reason = OFPER_IDLE_TIMEOUT;
232 } else if (flow->hard_timeout != OFP_FLOW_PERMANENT
233 && now > flow->created + flow->hard_timeout) {
234 flow->reason = OFPER_HARD_TIMEOUT;
241 void flow_used(struct sw_flow *flow, struct buffer *buffer)
243 flow->used = time_now();
244 flow->packet_count++;
245 flow->byte_count += buffer->size;