Implement userspace switch.
[sliver-openvswitch.git] / switch / switch-flow.c
1 /* Copyright (C) 2008 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #include "switch-flow.h"
23 #include <arpa/inet.h>
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "buffer.h"
28 #include "openflow.h"
29 #include "packets.h"
30
31 /* Internal function used to compare fields in flow. */
32 static inline
33 int flow_fields_match(const struct flow *a, const struct flow *b, uint16_t w)
34 {
35     return ((w & OFPFW_IN_PORT || a->in_port == b->in_port)
36             && (w & OFPFW_DL_VLAN || a->dl_vlan == b->dl_vlan)
37             && (w & OFPFW_DL_SRC || !memcmp(a->dl_src, b->dl_src, ETH_ADDR_LEN))
38             && (w & OFPFW_DL_DST || !memcmp(a->dl_dst, b->dl_dst, ETH_ADDR_LEN))
39             && (w & OFPFW_DL_TYPE || a->dl_type == b->dl_type)
40             && (w & OFPFW_NW_SRC || a->nw_src == b->nw_src)
41             && (w & OFPFW_NW_DST || a->nw_dst == b->nw_dst)
42             && (w & OFPFW_NW_PROTO || a->nw_proto == b->nw_proto)
43             && (w & OFPFW_TP_SRC || a->tp_src == b->tp_src)
44             && (w & OFPFW_TP_DST || a->tp_dst == b->tp_dst));
45 }
46
47 /* Returns nonzero if 'a' and 'b' match, that is, if their fields are equal
48  * modulo wildcards, zero otherwise. */
49 inline
50 int flow_matches(const struct sw_flow_key *a, const struct sw_flow_key *b)
51 {
52     return flow_fields_match(&a->flow, &b->flow, a->wildcards | b->wildcards);
53 }
54
55 /* Returns nonzero if 't' (the table entry's key) and 'd' (the key 
56  * describing the deletion) match, that is, if their fields are 
57  * equal modulo wildcards, zero otherwise.  If 'strict' is nonzero, the
58  * wildcards must match in both 't_key' and 'd_key'.  Note that the
59  * table's wildcards are ignored unless 'strict' is set. */
60 inline
61 int flow_del_matches(const struct sw_flow_key *t, const struct sw_flow_key *d, int strict)
62 {
63     if (strict && t->wildcards != d->wildcards)
64         return 0;
65
66     return flow_fields_match(&t->flow, &d->flow, d->wildcards);
67 }
68
69 void flow_extract_match(struct sw_flow_key* to, const struct ofp_match* from)
70 {
71     to->wildcards = ntohs(from->wildcards) & OFPFW_ALL;
72     to->flow.in_port   = from->in_port;
73     to->flow.dl_vlan   = from->dl_vlan;
74     memcpy(to->flow.dl_src, from->dl_src, ETH_ADDR_LEN);
75     memcpy(to->flow.dl_dst, from->dl_dst, ETH_ADDR_LEN);
76     to->flow.dl_type   = from->dl_type;
77     to->flow.nw_src   = from->nw_src;
78     to->flow.nw_dst   = from->nw_dst;
79     to->flow.nw_proto  = from->nw_proto;
80     to->flow.tp_src   = from->tp_src;
81     to->flow.tp_dst   = from->tp_dst;
82     to->flow.reserved = 0;
83 }
84
85 void flow_fill_match(struct ofp_match* to, const struct sw_flow_key* from)
86 {
87     to->wildcards = htons(from->wildcards);
88     to->in_port   = from->flow.in_port;
89     to->dl_vlan   = from->flow.dl_vlan;
90     memcpy(to->dl_src, from->flow.dl_src, ETH_ADDR_LEN);
91     memcpy(to->dl_dst, from->flow.dl_dst, ETH_ADDR_LEN);
92     to->dl_type   = from->flow.dl_type;
93     to->nw_src        = from->flow.nw_src;
94     to->nw_dst        = from->flow.nw_dst;
95     to->nw_proto  = from->flow.nw_proto;
96     to->tp_src        = from->flow.tp_src;
97     to->tp_dst        = from->flow.tp_dst;
98     memset(to->pad, '\0', sizeof(to->pad));
99 }
100
101 /* Allocates and returns a new flow with 'n_actions' action, using allocation
102  * flags 'flags'.  Returns the new flow or a null pointer on failure. */
103 struct sw_flow *flow_alloc(int n_actions)
104 {
105     struct sw_flow *flow = malloc(sizeof *flow);
106     if (!flow)
107         return NULL;
108
109     flow->n_actions = n_actions;
110     flow->actions = malloc(n_actions * sizeof *flow->actions);
111     if (!flow->actions && n_actions > 0) {
112         free(flow);
113         return NULL;
114     }
115     return flow;
116 }
117
118 /* Frees 'flow' immediately. */
119 void flow_free(struct sw_flow *flow)
120 {
121     if (!flow) {
122         return; 
123     }
124     free(flow->actions);
125     free(flow);
126 }
127
128 /* Prints a representation of 'key' to the kernel log. */
129 void print_flow(const struct sw_flow_key *key)
130 {
131     const struct flow *f = &key->flow;
132     printf("wild%04x port%04x:vlan%04x mac%02x:%02x:%02x:%02x:%02x:%02x"
133            "->%02x:%02x:%02x:%02x:%02x:%02x "
134            "proto%04x ip%u.%u.%u.%u->%u.%u.%u.%u port%d->%d\n",
135            key->wildcards, ntohs(f->in_port), ntohs(f->dl_vlan),
136            f->dl_src[0], f->dl_src[1], f->dl_src[2],
137            f->dl_src[3], f->dl_src[4], f->dl_src[5],
138            f->dl_dst[0], f->dl_dst[1], f->dl_dst[2],
139            f->dl_dst[3], f->dl_dst[4], f->dl_dst[5],
140            ntohs(f->dl_type),
141            ((unsigned char *)&f->nw_src)[0],
142            ((unsigned char *)&f->nw_src)[1],
143            ((unsigned char *)&f->nw_src)[2],
144            ((unsigned char *)&f->nw_src)[3],
145            ((unsigned char *)&f->nw_dst)[0],
146            ((unsigned char *)&f->nw_dst)[1],
147            ((unsigned char *)&f->nw_dst)[2],
148            ((unsigned char *)&f->nw_dst)[3],
149            ntohs(f->tp_src), ntohs(f->tp_dst));
150 }
151
152 int flow_timeout(struct sw_flow *flow)
153 {
154     if (flow->max_idle == OFP_FLOW_PERMANENT)
155         return 0;
156
157     /* FIXME */
158     return time(0) > flow->timeout;
159 }
160
161 void flow_used(struct sw_flow *flow, struct buffer *buffer)
162 {
163     if (flow->max_idle != OFP_FLOW_PERMANENT)
164         flow->timeout = time(0) + flow->max_idle;
165
166     flow->packet_count++;
167     flow->byte_count += buffer->size;
168 }