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