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>
41 #include "openflow/openflow.h"
42 #include "openflow/nicira-ext.h"
46 /* Internal function used to compare fields in flow. */
48 flow_fields_match(const struct flow *a, const struct flow *b, uint16_t w,
49 uint32_t src_mask, uint32_t dst_mask)
51 return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
52 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
53 && (w & OFPFW_DL_SRC || eth_addr_equals(a->dl_src, b->dl_src))
54 && (w & OFPFW_DL_DST || eth_addr_equals(a->dl_dst, b->dl_dst))
55 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
56 && !((a->nw_src ^ b->nw_src) & src_mask)
57 && !((a->nw_dst ^ b->nw_dst) & dst_mask)
58 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
59 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
60 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
63 static uint32_t make_nw_mask(int n_wild_bits)
65 n_wild_bits &= (1u << OFPFW_NW_SRC_BITS) - 1;
66 return n_wild_bits < 32 ? htonl(~((1u << n_wild_bits) - 1)) : 0;
69 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
70 * modulo wildcards in 'b', zero otherwise. */
72 flow_matches_1wild(const struct sw_flow_key *a, const struct sw_flow_key *b)
74 return flow_fields_match(&a->flow, &b->flow, b->wildcards,
75 b->nw_src_mask, b->nw_dst_mask);
78 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
79 * modulo wildcards in 'a' or 'b', zero otherwise. */
81 flow_matches_2wild(const struct sw_flow_key *a, const struct sw_flow_key *b)
83 return flow_fields_match(&a->flow, &b->flow, a->wildcards | b->wildcards,
84 a->nw_src_mask & b->nw_src_mask,
85 a->nw_dst_mask & b->nw_dst_mask);
88 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key
89 * describing the match) match, that is, if their fields are
90 * equal modulo wildcards, zero otherwise. If 'strict' is nonzero, the
91 * wildcards must match in both 't_key' and 'd_key'. Note that the
92 * table's wildcards are ignored unless 'strict' is set. */
94 flow_matches_desc(const struct sw_flow_key *t, const struct sw_flow_key *d,
97 if (strict && d->wildcards != t->wildcards) {
100 return flow_matches_1wild(t, d);
104 flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
106 to->wildcards = ntohl(from->wildcards) & OFPFW_ALL;
107 to->flow.reserved = 0;
108 to->flow.in_port = from->in_port;
109 to->flow.dl_vlan = from->dl_vlan;
110 memcpy(to->flow.dl_src, from->dl_src, ETH_ADDR_LEN);
111 memcpy(to->flow.dl_dst, from->dl_dst, ETH_ADDR_LEN);
112 to->flow.dl_type = from->dl_type;
114 to->flow.nw_src = to->flow.nw_dst = to->flow.nw_proto = 0;
115 to->flow.tp_src = to->flow.tp_dst = 0;
117 #define OFPFW_TP (OFPFW_TP_SRC | OFPFW_TP_DST)
118 #define OFPFW_NW (OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK | OFPFW_NW_PROTO)
119 if (to->wildcards & OFPFW_DL_TYPE) {
120 /* Can't sensibly match on network or transport headers if the
121 * data link type is unknown. */
122 to->wildcards |= OFPFW_NW | OFPFW_TP;
123 } else if (from->dl_type == htons(ETH_TYPE_IP)) {
124 to->flow.nw_src = from->nw_src;
125 to->flow.nw_dst = from->nw_dst;
126 to->flow.nw_proto = from->nw_proto;
128 if (to->wildcards & OFPFW_NW_PROTO) {
129 /* Can't sensibly match on transport headers if the network
130 * protocol is unknown. */
131 to->wildcards |= OFPFW_TP;
132 } else if (from->nw_proto == IPPROTO_TCP
133 || from->nw_proto == IPPROTO_UDP
134 || from->nw_proto == IPPROTO_ICMP) {
135 to->flow.tp_src = from->tp_src;
136 to->flow.tp_dst = from->tp_dst;
138 /* Transport layer fields are undefined. Mark them as
139 * exact-match to allow such flows to reside in table-hash,
140 * instead of falling into table-linear. */
141 to->wildcards &= ~OFPFW_TP;
144 /* Network and transport layer fields are undefined. Mark them
145 * as exact-match to allow such flows to reside in table-hash,
146 * instead of falling into table-linear. */
147 to->wildcards &= ~(OFPFW_NW | OFPFW_TP);
150 /* We set these late because code above adjusts to->wildcards. */
151 to->nw_src_mask = make_nw_mask(to->wildcards >> OFPFW_NW_SRC_SHIFT);
152 to->nw_dst_mask = make_nw_mask(to->wildcards >> OFPFW_NW_DST_SHIFT);
156 flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
158 to->wildcards = htonl(from->wildcards);
159 to->in_port = from->flow.in_port;
160 to->dl_vlan = from->flow.dl_vlan;
161 memcpy(to->dl_src, from->flow.dl_src, ETH_ADDR_LEN);
162 memcpy(to->dl_dst, from->flow.dl_dst, ETH_ADDR_LEN);
163 to->dl_type = from->flow.dl_type;
164 to->nw_src = from->flow.nw_src;
165 to->nw_dst = from->flow.nw_dst;
166 to->nw_proto = from->flow.nw_proto;
167 to->tp_src = from->flow.tp_src;
168 to->tp_dst = from->flow.tp_dst;
172 /* Allocates and returns a new flow with room for 'actions_len' actions.
173 * Returns the new flow or a null pointer on failure. */
175 flow_alloc(size_t actions_len)
177 struct sw_flow_actions *sfa;
178 size_t size = sizeof *sfa + actions_len;
179 struct sw_flow *flow = malloc(sizeof *flow);
188 sfa->actions_len = actions_len;
193 /* Frees 'flow' immediately. */
195 flow_free(struct sw_flow *flow)
204 /* Copies 'actions' into a newly allocated structure for use by 'flow'
205 * and frees the structure that defined the previous actions. */
206 void flow_replace_acts(struct sw_flow *flow,
207 const struct ofp_action_header *actions, size_t actions_len)
209 struct sw_flow_actions *sfa;
210 int size = sizeof *sfa + actions_len;
216 sfa->actions_len = actions_len;
217 memcpy(sfa->actions, actions, actions_len);
225 /* Prints a representation of 'key' to the kernel log. */
227 print_flow(const struct sw_flow_key *key)
229 const struct flow *f = &key->flow;
230 printf("wild%08x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
231 "->%02x:%02x:%02x:%02x:%02x:%02x "
232 "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
233 key->wildcards, ntohs(f->in_port), ntohs(f->dl_vlan),
234 f->dl_src[0], f->dl_src[1], f->dl_src[2],
235 f->dl_src[3], f->dl_src[4], f->dl_src[5],
236 f->dl_dst[0], f->dl_dst[1], f->dl_dst[2],
237 f->dl_dst[3], f->dl_dst[4], f->dl_dst[5],
239 ((unsigned char *)&f->nw_src)[0],
240 ((unsigned char *)&f->nw_src)[1],
241 ((unsigned char *)&f->nw_src)[2],
242 ((unsigned char *)&f->nw_src)[3],
243 ((unsigned char *)&f->nw_dst)[0],
244 ((unsigned char *)&f->nw_dst)[1],
245 ((unsigned char *)&f->nw_dst)[2],
246 ((unsigned char *)&f->nw_dst)[3],
247 ntohs(f->tp_src), ntohs(f->tp_dst));
250 bool flow_timeout(struct sw_flow *flow)
252 uint64_t now = time_msec();
253 if (flow->idle_timeout != OFP_FLOW_PERMANENT
254 && now > flow->used + flow->idle_timeout * 1000) {
255 flow->reason = NXFER_IDLE_TIMEOUT;
257 } else if (flow->hard_timeout != OFP_FLOW_PERMANENT
258 && now > flow->created + flow->hard_timeout * 1000) {
259 flow->reason = NXFER_HARD_TIMEOUT;
266 /* Returns nonzero if 'flow' contains an output action to 'out_port' or
267 * has the value OFPP_NONE. 'out_port' is in network-byte order. */
268 int flow_has_out_port(struct sw_flow *flow, uint16_t out_port)
270 struct sw_flow_actions *sf_acts = flow->sf_acts;
271 size_t actions_len = sf_acts->actions_len;
272 uint8_t *p = (uint8_t *)sf_acts->actions;
274 if (out_port == htons(OFPP_NONE))
277 while (actions_len > 0) {
278 struct ofp_action_header *ah = (struct ofp_action_header *)p;
279 size_t len = ntohs(ah->len);
281 if (ah->type == htons(OFPAT_OUTPUT)) {
282 struct ofp_action_output *oa = (struct ofp_action_output *)p;
283 if (oa->port == out_port) {
294 void flow_used(struct sw_flow *flow, struct ofpbuf *buffer)
296 flow->used = time_msec();
298 if (flow->key.flow.dl_type == htons(ETH_TYPE_IP)) {
299 struct ip_header *nh = buffer->l3;
300 flow->ip_tos = nh->ip_tos;
302 if (flow->key.flow.nw_proto == IP_TYPE_TCP) {
303 struct tcp_header *th = buffer->l4;
304 flow->tcp_flags |= TCP_FLAGS(th->tcp_ctl);
308 flow->packet_count++;
309 flow->byte_count += buffer->size;