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