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.
34 #include "switch-flow.h"
35 #include <arpa/inet.h>
43 /* Internal function used to compare fields in flow. */
45 int flow_fields_match(const struct flow *a, const struct flow *b, uint16_t w)
47 return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
48 && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
49 && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ADDR_LEN))
50 && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ADDR_LEN))
51 && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
52 && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
53 && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
54 && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
55 && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
56 && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
59 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
60 * modulo wildcards, zero otherwise. */
62 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
64 return flow_fields_match(&a->flow, &b->flow, a->wildcards | b->wildcards);
67 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key
68 * describing the deletion) match, that is, if their fields are
69 * equal modulo wildcards, zero otherwise. If 'strict' is nonzero, the
70 * wildcards must match in both 't_key' and 'd_key'. Note that the
71 * table's wildcards are ignored unless 'strict' is set. */
73 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
75 if (strict && t->wildcards != d->wildcards)
78 return flow_fields_match(&t->flow, &d->flow, d->wildcards);
81 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
83 to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
84 to->flow.reserved = 0;
85 to->flow.in_port = from->in_port;
86 to->flow.dl_vlan = from->dl_vlan;
87 memcpy(to->flow.dl_src, from->dl_src, ETH_ADDR_LEN);
88 memcpy(to->flow.dl_dst, from->dl_dst, ETH_ADDR_LEN);
89 to->flow.dl_type = from->dl_type;
92 if (from->dl_type == htons(ETH_TYPE_IP)) {
93 to->flow.nw_src = from->nw_src;
94 to->flow.nw_dst = from->nw_dst;
95 to->flow.nw_proto = from->nw_proto;
97 if ((from->nw_proto != IPPROTO_TCP && from->nw_proto != IPPROTO_UDP)) {
100 to->flow.tp_src = from->tp_src;
101 to->flow.tp_dst = from->tp_dst;
105 /* dl_type is not IPv4, so the network layer fields are undefined. We set
106 * them to zero and mark them as exact-match to allow such flows to reside
107 * in table-hash, instead of falling into table-linear. */
108 to->wildcards &= ~(OFPFW_NW_SRC | OFPFW_NW_DST | OFPFW_NW_PROTO);
111 to->flow.nw_proto = 0;
114 /* nw_proto is not TCP or UDP, so the transport layer fields are undefined.
115 * We set them to zero and mark them as exact-match to allow such flows to
116 * reside in table-hash, instead of falling into table-linear. */
117 to->wildcards &= ~(OFPFW_TP_SRC | OFPFW_TP_DST);
122 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
124 to->wildcards = htons(from->wildcards);
125 to->in_port = from->flow.in_port;
126 to->dl_vlan = from->flow.dl_vlan;
127 memcpy(to->dl_src, from->flow.dl_src, ETH_ADDR_LEN);
128 memcpy(to->dl_dst, from->flow.dl_dst, ETH_ADDR_LEN);
129 to->dl_type = from->flow.dl_type;
130 to->nw_src = from->flow.nw_src;
131 to->nw_dst = from->flow.nw_dst;
132 to->nw_proto = from->flow.nw_proto;
133 to->tp_src = from->flow.tp_src;
134 to->tp_dst = from->flow.tp_dst;
135 memset(to->pad, '\0', sizeof(to->pad));
138 /* Allocates and returns a new flow with 'n_actions' action, using allocation
139 * flags 'flags'. Returns the new flow or a null pointer on failure. */
140 struct sw_flow *flow_alloc(int n_actions)
142 struct sw_flow *flow = malloc(sizeof *flow);
146 flow->n_actions = n_actions;
147 flow->actions = malloc(n_actions * sizeof *flow->actions);
148 if (!flow->actions && n_actions > 0) {
155 /* Frees 'flow' immediately. */
156 void flow_free(struct sw_flow *flow)
165 /* Prints a representation of 'key' to the kernel log. */
166 void print_flow(const struct sw_flow_key *key)
168 const struct flow *f = &key->flow;
169 printf("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
170 "->%02x:%02x:%02x:%02x:%02x:%02x "
171 "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
172 key->wildcards, ntohs(f->in_port), ntohs(f->dl_vlan),
173 f->dl_src[0], f->dl_src[1], f->dl_src[2],
174 f->dl_src[3], f->dl_src[4], f->dl_src[5],
175 f->dl_dst[0], f->dl_dst[1], f->dl_dst[2],
176 f->dl_dst[3], f->dl_dst[4], f->dl_dst[5],
178 ((unsigned char *)&f->nw_src)[0],
179 ((unsigned char *)&f->nw_src)[1],
180 ((unsigned char *)&f->nw_src)[2],
181 ((unsigned char *)&f->nw_src)[3],
182 ((unsigned char *)&f->nw_dst)[0],
183 ((unsigned char *)&f->nw_dst)[1],
184 ((unsigned char *)&f->nw_dst)[2],
185 ((unsigned char *)&f->nw_dst)[3],
186 ntohs(f->tp_src), ntohs(f->tp_dst));
189 int flow_timeout(struct sw_flow *flow)
191 if (flow->max_idle == OFP_FLOW_PERMANENT)
195 return time(0) > flow->timeout;
198 void flow_used(struct sw_flow *flow, struct buffer *buffer)
200 if (flow->max_idle != OFP_FLOW_PERMANENT)
201 flow->timeout = time(0) + flow->max_idle;
203 flow->packet_count++;
204 flow->byte_count += buffer->size;