cc7f13939262fea2d219014864a3ce6e49613217
[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 <config.h>
35 #include "learning-switch.h"
36
37 #include <errno.h>
38 #include <inttypes.h>
39 #include <netinet/in.h>
40 #include <stdlib.h>
41 #include <time.h>
42
43 #include "flow.h"
44 #include "mac-learning.h"
45 #include "ofpbuf.h"
46 #include "ofp-print.h"
47 #include "openflow.h"
48 #include "queue.h"
49 #include "rconn.h"
50 #include "stp.h"
51 #include "timeval.h"
52 #include "vconn.h"
53 #include "xtoxll.h"
54
55 #define THIS_MODULE VLM_learning_switch
56 #include "vlog.h"
57
58 struct lswitch {
59     /* If nonnegative, the switch sets up flows that expire after the given
60      * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
61      * Otherwise, the switch processes every packet. */
62     int max_idle;
63
64     uint64_t datapath_id;
65     uint32_t capabilities;
66     time_t last_features_request;
67     struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
68
69     /* Number of outgoing queued packets on the rconn. */
70     int n_queued;
71 };
72
73 /* The log messages here could actually be useful in debugging, so keep the
74  * rate limit relatively high. */
75 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
76
77 static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
78 static void send_features_request(struct lswitch *, struct rconn *);
79 static void process_switch_features(struct lswitch *, struct rconn *,
80                                     struct ofp_switch_features *);
81 static void process_packet_in(struct lswitch *, struct rconn *,
82                               struct ofp_packet_in *);
83 static void process_echo_request(struct lswitch *, struct rconn *,
84                                  struct ofp_header *);
85 static void process_port_status(struct lswitch *, struct rconn *,
86                                 struct ofp_port_status *);
87 static void process_phy_port(struct lswitch *, struct rconn *,
88                              const struct ofp_phy_port *);
89
90 /* Creates and returns a new learning switch.
91  *
92  * If 'learn_macs' is true, the new switch will learn the ports on which MAC
93  * addresses appear.  Otherwise, the new switch will flood all packets.
94  *
95  * If 'max_idle' is nonnegative, the new switch will set up flows that expire
96  * after the given number of seconds (or never expire, if 'max_idle' is
97  * OFP_FLOW_PERMANENT).  Otherwise, the new switch will process every packet.
98  *
99  * 'rconn' is used to send out an OpenFlow features request. */
100 struct lswitch *
101 lswitch_create(struct rconn *rconn, bool learn_macs, int max_idle)
102 {
103     struct lswitch *sw = xcalloc(1, sizeof *sw);
104     sw->max_idle = max_idle;
105     sw->datapath_id = 0;
106     sw->last_features_request = time_now() - 1;
107     sw->ml = learn_macs ? mac_learning_create() : NULL;
108     send_features_request(sw, rconn);
109     return sw;
110 }
111
112 /* Destroys 'sw'. */
113 void
114 lswitch_destroy(struct lswitch *sw)
115 {
116     if (sw) {
117         mac_learning_destroy(sw->ml);
118         free(sw);
119     }
120 }
121
122 static size_t
123 min_size(uint8_t type)
124 {
125     return (type == OFPT_FEATURES_REPLY ? sizeof(struct ofp_switch_features)
126             : type == OFPT_PACKET_IN ? offsetof (struct ofp_packet_in, data)
127             : type == OFPT_PORT_STATUS ? sizeof(struct ofp_port_status)
128             : sizeof(struct ofp_header));
129 }
130
131 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
132  * to the learning switch state in 'sw'.  The most likely result of processing
133  * is that flow-setup and packet-out OpenFlow messages will be sent out on
134  * 'rconn'.  */
135 void
136 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
137                        const struct ofpbuf *msg)
138 {
139     struct ofp_header *oh;
140
141     oh = msg->data;
142     if (msg->size < min_size(oh->type)) {
143         VLOG_WARN_RL(&rl,
144                      "%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
145                      rconn_get_name(rconn),
146                      msg->size, oh->type, min_size(oh->type));
147         return;
148     }
149
150     if (oh->type == OFPT_ECHO_REQUEST) {
151         process_echo_request(sw, rconn, msg->data);
152     } else if (oh->type == OFPT_FEATURES_REPLY) {
153         process_switch_features(sw, rconn, msg->data);
154     } else if (sw->datapath_id == 0) {
155         send_features_request(sw, rconn);
156     } else if (oh->type == OFPT_PACKET_IN) {
157         process_packet_in(sw, rconn, msg->data);
158     } else if (oh->type == OFPT_PORT_STATUS) {
159         process_port_status(sw, rconn, msg->data);
160     } else {
161         if (VLOG_IS_DBG_ENABLED()) {
162             char *p = ofp_to_string(msg->data, msg->size, 2);
163             VLOG_DBG_RL(&rl, "OpenFlow packet ignored: %s", p);
164             free(p);
165         }
166     }
167 }
168 \f
169 static void
170 send_features_request(struct lswitch *sw, struct rconn *rconn)
171 {
172     time_t now = time_now();
173     if (now >= sw->last_features_request + 1) {
174         struct ofpbuf *b;
175         struct ofp_switch_config *osc;
176
177         /* Send OFPT_FEATURES_REQUEST. */
178         make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
179         queue_tx(sw, rconn, b);
180
181         /* Send OFPT_SET_CONFIG. */
182         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
183         osc->flags = htons(OFPC_SEND_FLOW_EXP);
184         osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
185         queue_tx(sw, rconn, b);
186
187         sw->last_features_request = now;
188     }
189 }
190
191 static void
192 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
193 {
194     int retval = rconn_send_with_limit(rconn, b, &sw->n_queued, 10);
195     if (retval && retval != ENOTCONN) {
196         if (retval == EAGAIN) {
197             VLOG_WARN_RL(&rl, "%s: tx queue overflow", rconn_get_name(rconn));
198         } else {
199             VLOG_WARN_RL(&rl, "%s: send: %s",
200                          rconn_get_name(rconn), strerror(retval));
201         }
202     }
203 }
204
205 static void
206 process_switch_features(struct lswitch *sw, struct rconn *rconn,
207                         struct ofp_switch_features *osf)
208 {
209     size_t n_ports = ((ntohs(osf->header.length)
210                        - offsetof(struct ofp_switch_features, ports))
211                       / sizeof *osf->ports);
212     size_t i;
213
214     sw->datapath_id = osf->datapath_id;
215     sw->capabilities = ntohl(osf->capabilities);
216     for (i = 0; i < n_ports; i++) {
217         process_phy_port(sw, rconn, &osf->ports[i]);
218     }
219 }
220
221 static void
222 process_packet_in(struct lswitch *sw, struct rconn *rconn,
223                   struct ofp_packet_in *opi)
224 {
225     uint16_t in_port = ntohs(opi->in_port);
226     uint16_t out_port = OFPP_FLOOD;
227
228     size_t pkt_ofs, pkt_len;
229     struct ofpbuf pkt;
230     struct flow flow;
231
232     /* Extract flow data from 'opi' into 'flow'. */
233     pkt_ofs = offsetof(struct ofp_packet_in, data);
234     pkt_len = ntohs(opi->header.length) - pkt_ofs;
235     pkt.data = opi->data;
236     pkt.size = pkt_len;
237     flow_extract(&pkt, in_port, &flow);
238
239     if (sw->ml) {
240         if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) {
241             VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on datapath %"
242                         PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src),
243                         ntohll(sw->datapath_id), in_port);
244         }
245         out_port = mac_learning_lookup(sw->ml, flow.dl_dst);
246     }
247
248     if (in_port == out_port) {
249         /* The input and output port match.  Set up a flow to drop packets. */
250         queue_tx(sw, rconn, make_add_flow(&flow, ntohl(opi->buffer_id),
251                                           sw->max_idle, 0));
252     } else if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
253         /* The output port is known, or we always flood everything, so add a
254          * new flow. */
255         queue_tx(sw, rconn, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
256                                                  out_port, sw->max_idle));
257
258         /* If the switch didn't buffer the packet, we need to send a copy. */
259         if (ntohl(opi->buffer_id) == UINT32_MAX) {
260             queue_tx(sw, rconn,
261                      make_unbuffered_packet_out(&pkt, in_port, out_port));
262         }
263     } else {
264         /* We don't know that MAC, or we don't set up flows.  Send along the
265          * packet without setting up a flow. */
266         struct ofpbuf *b;
267         if (ntohl(opi->buffer_id) == UINT32_MAX) {
268             b = make_unbuffered_packet_out(&pkt, in_port, out_port);
269         } else {
270             b = make_buffered_packet_out(ntohl(opi->buffer_id),
271                                          in_port, out_port);
272         }
273         queue_tx(sw, rconn, b);
274     }
275 }
276
277 static void
278 process_echo_request(struct lswitch *sw, struct rconn *rconn,
279                      struct ofp_header *rq)
280 {
281     queue_tx(sw, rconn, make_echo_reply(rq));
282 }
283
284 static void
285 process_port_status(struct lswitch *sw, struct rconn *rconn,
286                     struct ofp_port_status *ops)
287 {
288     process_phy_port(sw, rconn, &ops->desc);
289 }
290
291 static void
292 process_phy_port(struct lswitch *sw, struct rconn *rconn,
293                  const struct ofp_phy_port *opp)
294 {
295     if (sw->capabilities & OFPC_STP && ntohs(opp->port_no) < STP_MAX_PORTS) {
296         uint32_t config = ntohl(opp->config);
297         uint32_t state = ntohl(opp->state);
298         uint32_t new_config = config & ~(OFPPC_NO_RECV | OFPPC_NO_RECV_STP
299                                          | OFPPC_NO_FWD | OFPPC_NO_PACKET_IN);
300         if (!(config & (OFPPC_NO_STP | OFPPC_PORT_DOWN))
301                     && !(state & OFPPS_LINK_DOWN)) {
302             bool forward = false;
303             bool learn = false;
304             switch (state & OFPPS_STP_MASK) {
305             case OFPPS_STP_LISTEN:
306             case OFPPS_STP_BLOCK:
307                 break;
308             case OFPPS_STP_LEARN:
309                 learn = true;
310                 break;
311             case OFPPS_STP_FORWARD:
312                 forward = learn = true;
313                 break;
314             }
315             if (!forward) {
316                 new_config |= OFPPC_NO_RECV | OFPPC_NO_FWD;
317             }
318             if (!learn) {
319                 new_config |= OFPPC_NO_PACKET_IN;
320             }
321         }
322         if (config != new_config) {
323             struct ofp_port_mod *opm;
324             struct ofpbuf *b;
325             int retval;
326
327             VLOG_WARN("port %d: config=%x new_config=%x",
328                       ntohs(opp->port_no), config, new_config);
329             opm = make_openflow(sizeof *opm, OFPT_PORT_MOD, &b);
330             opm->port_no = opp->port_no;
331             memcpy(opm->hw_addr, opp->hw_addr, OFP_ETH_ALEN);
332             opm->config = htonl(new_config);
333             opm->mask = htonl(config ^ new_config);
334             opm->advertise = htonl(0);
335             retval = rconn_send(rconn, b, NULL);
336             if (retval) {
337                 if (retval != ENOTCONN) {
338                     VLOG_WARN_RL(&rl, "%s: send: %s",
339                                  rconn_get_name(rconn), strerror(retval));
340                 }
341                 ofpbuf_delete(b);
342             }
343         }
344     }
345 }