3b432d3b288351e7e260a32bd01ba8201598fd38
[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     /* If nonnegative, the switch sets up flows that expire after the given
57      * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
58      * Otherwise, the switch processes every packet. */
59     int max_idle;
60
61     uint64_t datapath_id;
62     time_t last_features_request;
63     struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
64 };
65
66 static void queue_tx(struct lswitch *, struct rconn *, struct buffer *);
67 static void send_features_request(struct lswitch *, struct rconn *);
68 static void process_packet_in(struct lswitch *, struct rconn *,
69                               struct ofp_packet_in *);
70 static void process_echo_request(struct lswitch *, struct rconn *,
71                                  struct ofp_header *);
72
73 /* Creates and returns a new learning switch.
74  *
75  * If 'learn_macs' is true, the new switch will learn the ports on which MAC
76  * addresses appear.  Otherwise, the new switch will flood all packets.
77  *
78  * If 'max_idle' is nonnegative, the new switch will set up flows that expire
79  * after the given number of seconds (or never expire, if 'max_idle' is
80  * OFP_FLOW_PERMANENT).  Otherwise, the new switch will process every packet.
81  *
82  * 'rconn' is used to send out an OpenFlow features request. */
83 struct lswitch *
84 lswitch_create(struct rconn *rconn, bool learn_macs, int max_idle)
85 {
86     struct lswitch *sw = xmalloc(sizeof *sw);
87     memset(sw, 0, sizeof *sw);
88     sw->max_idle = max_idle;
89     sw->datapath_id = 0;
90     sw->last_features_request = time(0) - 1;
91     sw->ml = learn_macs ? mac_learning_create() : NULL;
92     send_features_request(sw, rconn);
93     return sw;
94 }
95
96 /* Destroys 'sw'. */
97 void
98 lswitch_destroy(struct lswitch *sw)
99 {
100     if (sw) {
101         mac_learning_destroy(sw->ml);
102         free(sw);
103     }
104 }
105
106 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
107  * to the learning switch state in 'sw'.  The most likely result of processing
108  * is that flow-setup and packet-out OpenFlow messages will be sent out on
109  * 'rconn'.  */
110 void
111 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
112                        const struct buffer *msg)
113 {
114     static const size_t min_size[UINT8_MAX + 1] = {
115         [0 ... UINT8_MAX] = sizeof (struct ofp_header),
116         [OFPT_FEATURES_REPLY] = sizeof (struct ofp_switch_features),
117         [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data),
118     };
119     struct ofp_header *oh;
120
121     oh = msg->data;
122     if (msg->size < min_size[oh->type]) {
123         VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
124                   rconn_get_name(rconn),
125                   msg->size, oh->type, min_size[oh->type]);
126         return;
127     }
128
129     if (oh->type == OFPT_ECHO_REQUEST) {
130         process_echo_request(sw, rconn, msg->data);
131     } else if (oh->type == OFPT_FEATURES_REPLY) {
132         struct ofp_switch_features *osf = msg->data;
133         sw->datapath_id = osf->datapath_id;
134     } else if (sw->datapath_id == 0) {
135         send_features_request(sw, rconn);
136     } else if (oh->type == OFPT_PACKET_IN) {
137         process_packet_in(sw, rconn, msg->data);
138     } else {
139         if (VLOG_IS_DBG_ENABLED()) {
140             char *p = ofp_to_string(msg->data, msg->size, 2);
141             VLOG_DBG("OpenFlow packet ignored: %s", p);
142             free(p);
143         }
144     }
145 }
146 \f
147 static void
148 send_features_request(struct lswitch *sw, struct rconn *rconn)
149 {
150     time_t now = time(0);
151     if (now >= sw->last_features_request + 1) {
152         struct buffer *b;
153         struct ofp_header *ofr;
154         struct ofp_switch_config *osc;
155
156         /* Send OFPT_FEATURES_REQUEST. */
157         b = buffer_new(0);
158         ofr = buffer_put_uninit(b, sizeof *ofr);
159         memset(ofr, 0, sizeof *ofr);
160         ofr->type = OFPT_FEATURES_REQUEST;
161         ofr->version = OFP_VERSION;
162         ofr->length = htons(sizeof *ofr);
163         queue_tx(sw, rconn, b);
164
165         /* Send OFPT_SET_CONFIG. */
166         b = buffer_new(0);
167         osc = buffer_put_uninit(b, sizeof *osc);
168         memset(osc, 0, sizeof *osc);
169         osc->header.type = OFPT_SET_CONFIG;
170         osc->header.version = OFP_VERSION;
171         osc->header.length = htons(sizeof *osc);
172         osc->flags = htons(OFPC_SEND_FLOW_EXP);
173         osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
174         queue_tx(sw, rconn, b);
175
176         sw->last_features_request = now;
177     }
178 }
179
180 static void
181 queue_tx(struct lswitch *sw, struct rconn *rconn, struct buffer *b)
182 {
183     int retval = rconn_send(rconn, b);
184     if (retval) {
185         if (retval == EAGAIN) {
186             /* FIXME: ratelimit. */
187             VLOG_WARN("%s: tx queue overflow", rconn_get_name(rconn));
188         } else if (retval == ENOTCONN) {
189             /* Ignore. */
190         } else {
191             /* FIXME: ratelimit. */
192             VLOG_WARN("%s: send: %s", rconn_get_name(rconn), strerror(retval));
193         }
194         buffer_delete(b);
195     }
196 }
197
198 static void
199 process_packet_in(struct lswitch *sw, struct rconn *rconn,
200                   struct ofp_packet_in *opi)
201 {
202     uint16_t in_port = ntohs(opi->in_port);
203     uint16_t out_port = OFPP_FLOOD;
204
205     size_t pkt_ofs, pkt_len;
206     struct buffer pkt;
207     struct flow flow;
208
209     /* Extract flow data from 'opi' into 'flow'. */
210     pkt_ofs = offsetof(struct ofp_packet_in, data);
211     pkt_len = ntohs(opi->header.length) - pkt_ofs;
212     pkt.data = opi->data;
213     pkt.size = pkt_len;
214     flow_extract(&pkt, in_port, &flow);
215
216     if (sw->ml) {
217         if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) {
218             VLOG_DBG("learned that "ETH_ADDR_FMT" is on datapath %"
219                      PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src),
220                      ntohll(sw->datapath_id), in_port);
221         }
222         out_port = mac_learning_lookup(sw->ml, flow.dl_dst);
223     }
224
225     if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
226         /* The output port is known, or we always flood everything, so add a
227          * new flow. */
228         queue_tx(sw, rconn, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
229                                                  out_port, sw->max_idle));
230
231         /* If the switch didn't buffer the packet, we need to send a copy. */
232         if (ntohl(opi->buffer_id) == UINT32_MAX) {
233             queue_tx(sw, rconn,
234                      make_unbuffered_packet_out(&pkt, in_port, out_port));
235         }
236     } else {
237         /* We don't know that MAC, or we don't set up flows.  Send along the
238          * packet without setting up a flow. */
239         struct buffer *b;
240         if (ntohl(opi->buffer_id) == UINT32_MAX) {
241             b = make_unbuffered_packet_out(&pkt, in_port, out_port);
242         } else {
243             b = make_buffered_packet_out(ntohl(opi->buffer_id),
244                                          in_port, out_port);
245         }
246         queue_tx(sw, rconn, b);
247     }
248 }
249
250 static void
251 process_echo_request(struct lswitch *sw, struct rconn *rconn,
252                      struct ofp_header *rq)
253 {
254     queue_tx(sw, rconn, make_echo_reply(rq));
255 }