Factor learning switch out of controller into library.
[sliver-openvswitch.git] / lib / learning-switch.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  *
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:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
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
27  * SOFTWARE.
28  *
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.
32  */
33
34 #include "learning-switch.h"
35
36 #include <errno.h>
37 #include <inttypes.h>
38 #include <netinet/in.h>
39 #include <stdlib.h>
40 #include <time.h>
41
42 #include "buffer.h"
43 #include "flow.h"
44 #include "mac-learning.h"
45 #include "ofp-print.h"
46 #include "openflow.h"
47 #include "queue.h"
48 #include "rconn.h"
49 #include "vconn.h"
50 #include "xtoxll.h"
51
52 #define THIS_MODULE VLM_learning_switch
53 #include "vlog.h"
54
55 struct lswitch {
56     bool setup_flows; /* Set up flows? (or controller processes all packets) */
57     uint64_t datapath_id;
58     time_t last_features_request;
59     struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
60 };
61
62 static void queue_tx(struct lswitch *, struct rconn *, struct buffer *);
63 static void send_features_request(struct lswitch *, struct rconn *);
64 static void process_packet_in(struct lswitch *, struct rconn *,
65                               struct ofp_packet_in *);
66
67 /* Creates and returns a new learning switch.
68  *
69  * If 'learn_macs' is true, the new switch will learn the ports on which MAC
70  * addresses appear.  Otherwise, the new switch will flood all packets.
71  *
72  * If 'setup_flows' is true, the new switch will set up flows.  Otherwise, the
73  * new switch will process every packet.
74  *
75  * 'rconn' is used to send out an OpenFlow features request. */
76 struct lswitch *
77 lswitch_create(struct rconn *rconn, bool learn_macs, bool setup_flows)
78 {
79     struct lswitch *sw = xmalloc(sizeof *sw);
80     memset(sw, 0, sizeof *sw);
81     sw->setup_flows = setup_flows;
82     sw->datapath_id = 0;
83     sw->last_features_request = 0;
84     sw->ml = learn_macs ? mac_learning_create() : NULL;
85     send_features_request(sw, rconn);
86     return sw;
87 }
88
89 /* Destroys 'sw'. */
90 void
91 lswitch_destroy(struct lswitch *sw)
92 {
93     if (sw) {
94         mac_learning_destroy(sw->ml);
95         free(sw);
96     }
97 }
98
99 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
100  * to the learning switch state in 'sw'.  The most likely result of processing
101  * is that flow-setup and packet-out OpenFlow messages will be sent out on
102  * 'rconn'.  */
103 void
104 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
105                        const struct buffer *msg)
106 {
107     static const size_t min_size[UINT8_MAX + 1] = {
108         [0 ... UINT8_MAX] = sizeof (struct ofp_header),
109         [OFPT_FEATURES_REPLY] = sizeof (struct ofp_switch_features),
110         [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data),
111     };
112     struct ofp_header *oh;
113
114     oh = msg->data;
115     if (msg->size < min_size[oh->type]) {
116         VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
117                   rconn_get_name(rconn),
118                   msg->size, oh->type, min_size[oh->type]);
119         return;
120     }
121
122     if (oh->type == OFPT_FEATURES_REPLY) {
123         struct ofp_switch_features *osf = msg->data;
124         sw->datapath_id = osf->datapath_id;
125     } else if (sw->datapath_id == 0) {
126         send_features_request(sw, rconn);
127     } else if (oh->type == OFPT_PACKET_IN) {
128         process_packet_in(sw, rconn, msg->data);
129     } else {
130         if (VLOG_IS_DBG_ENABLED()) {
131             char *p = ofp_to_string(msg->data, msg->size, 2);
132             VLOG_DBG("OpenFlow packet ignored: %s", p);
133             free(p);
134         }
135     }
136 }
137 \f
138 static void
139 send_features_request(struct lswitch *sw, struct rconn *rconn)
140 {
141     time_t now = time(0);
142     if (now >= sw->last_features_request + 1) {
143         struct buffer *b;
144         struct ofp_header *ofr;
145         struct ofp_switch_config *osc;
146
147         /* Send OFPT_FEATURES_REQUEST. */
148         b = buffer_new(0);
149         ofr = buffer_put_uninit(b, sizeof *ofr);
150         memset(ofr, 0, sizeof *ofr);
151         ofr->type = OFPT_FEATURES_REQUEST;
152         ofr->version = OFP_VERSION;
153         ofr->length = htons(sizeof *ofr);
154         queue_tx(sw, rconn, b);
155
156         /* Send OFPT_SET_CONFIG. */
157         b = buffer_new(0);
158         osc = buffer_put_uninit(b, sizeof *osc);
159         memset(osc, 0, sizeof *osc);
160         osc->header.type = OFPT_SET_CONFIG;
161         osc->header.version = OFP_VERSION;
162         osc->header.length = htons(sizeof *osc);
163         osc->flags = htons(OFPC_SEND_FLOW_EXP);
164         osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
165         queue_tx(sw, rconn, b);
166
167         sw->last_features_request = now;
168     }
169 }
170
171 static void
172 queue_tx(struct lswitch *sw, struct rconn *rconn, struct buffer *b)
173 {
174     int retval = rconn_send(rconn, b);
175     if (retval) {
176         if (retval == EAGAIN) {
177             /* FIXME: ratelimit. */
178             VLOG_WARN("%s: tx queue overflow", rconn_get_name(rconn));
179         } else if (retval == ENOTCONN) {
180             /* Ignore. */
181         } else {
182             /* FIXME: ratelimit. */
183             VLOG_WARN("%s: send: %s", rconn_get_name(rconn), strerror(retval));
184         }
185         buffer_delete(b);
186     }
187 }
188
189 static void
190 process_packet_in(struct lswitch *sw, struct rconn *rconn,
191                   struct ofp_packet_in *opi)
192 {
193     uint16_t in_port = ntohs(opi->in_port);
194     uint16_t out_port = OFPP_FLOOD;
195
196     size_t pkt_ofs, pkt_len;
197     struct buffer pkt;
198     struct flow flow;
199
200     /* Extract flow data from 'opi' into 'flow'. */
201     pkt_ofs = offsetof(struct ofp_packet_in, data);
202     pkt_len = ntohs(opi->header.length) - pkt_ofs;
203     pkt.data = opi->data;
204     pkt.size = pkt_len;
205     flow_extract(&pkt, in_port, &flow);
206
207     if (sw->ml) {
208         if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) {
209             VLOG_DBG("learned that "ETH_ADDR_FMT" is on datapath %"
210                      PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src),
211                      ntohll(sw->datapath_id), in_port);
212         }
213         out_port = mac_learning_lookup(sw->ml, flow.dl_dst);
214     }
215
216     if (sw->setup_flows && (!sw->ml || out_port != OFPP_FLOOD)) {
217         /* The output port is known, or we always flood everything, so add a
218          * new flow. */
219         queue_tx(sw, rconn, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
220                                                  out_port));
221
222         /* If the switch didn't buffer the packet, we need to send a copy. */
223         if (ntohl(opi->buffer_id) == UINT32_MAX) {
224             queue_tx(sw, rconn,
225                      make_unbuffered_packet_out(&pkt, in_port, out_port));
226         }
227     } else {
228         /* We don't know that MAC, or we don't set up flows.  Send along the
229          * packet without setting up a flow. */
230         struct buffer *b;
231         if (ntohl(opi->buffer_id) == UINT32_MAX) {
232             b = make_unbuffered_packet_out(&pkt, in_port, out_port);
233         } else {
234             b = make_buffered_packet_out(ntohl(opi->buffer_id),
235                                          in_port, out_port);
236         }
237         queue_tx(sw, rconn, b);
238     }
239 }