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