90749c018c6320b699e2cf2e0a4f5d0c4d9d2dc8
[sliver-openvswitch.git] / lib / learning-switch.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "learning-switch.h"
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include "flow.h"
27 #include "mac-learning.h"
28 #include "ofpbuf.h"
29 #include "ofp-parse.h"
30 #include "ofp-print.h"
31 #include "ofp-util.h"
32 #include "openflow/openflow.h"
33 #include "poll-loop.h"
34 #include "queue.h"
35 #include "rconn.h"
36 #include "timeval.h"
37 #include "vconn.h"
38 #include "vlog.h"
39 #include "xtoxll.h"
40
41 VLOG_DEFINE_THIS_MODULE(learning_switch)
42
43 struct lswitch {
44     /* If nonnegative, the switch sets up flows that expire after the given
45      * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
46      * Otherwise, the switch processes every packet. */
47     int max_idle;
48
49     unsigned long long int datapath_id;
50     time_t last_features_request;
51     struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
52     uint32_t wildcards;         /* Wildcards to apply to flows. */
53     bool action_normal;         /* Use OFPP_NORMAL? */
54     uint32_t queue;             /* OpenFlow queue to use, or UINT32_MAX. */
55
56     /* Number of outgoing queued packets on the rconn. */
57     struct rconn_packet_counter *queued;
58 };
59
60 /* The log messages here could actually be useful in debugging, so keep the
61  * rate limit relatively high. */
62 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
63
64 static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
65 static void send_features_request(struct lswitch *, struct rconn *);
66
67 typedef void packet_handler_func(struct lswitch *, struct rconn *, void *);
68 static packet_handler_func process_switch_features;
69 static packet_handler_func process_packet_in;
70 static packet_handler_func process_echo_request;
71
72 /* Creates and returns a new learning switch whose configuration is given by
73  * 'cfg'.
74  *
75  * 'rconn' is used to send out an OpenFlow features request. */
76 struct lswitch *
77 lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
78 {
79     const struct ofpbuf *b;
80     struct lswitch *sw;
81
82     sw = xzalloc(sizeof *sw);
83     sw->max_idle = cfg->max_idle;
84     sw->datapath_id = 0;
85     sw->last_features_request = time_now() - 1;
86     sw->ml = cfg->mode == LSW_LEARN ? mac_learning_create() : NULL;
87     sw->action_normal = cfg->mode == LSW_NORMAL;
88     if (cfg->exact_flows) {
89         /* Exact match. */
90         sw->wildcards = 0;
91     } else {
92         /* We cannot wildcard all fields.
93          * We need in_port to detect moves.
94          * We need both SA and DA to do learning. */
95         sw->wildcards = (OFPFW_DL_TYPE | OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK
96                          | OFPFW_NW_PROTO | OFPFW_TP_SRC | OFPFW_TP_DST);
97     }
98     sw->queue = cfg->queue_id;
99     sw->queued = rconn_packet_counter_create();
100     send_features_request(sw, rconn);
101
102     for (b = cfg->default_flows; b; b = b->next) {
103         queue_tx(sw, rconn, ofpbuf_clone(b));
104     }
105
106     return sw;
107 }
108
109 /* Destroys 'sw'. */
110 void
111 lswitch_destroy(struct lswitch *sw)
112 {
113     if (sw) {
114         mac_learning_destroy(sw->ml);
115         rconn_packet_counter_destroy(sw->queued);
116         free(sw);
117     }
118 }
119
120 /* Takes care of necessary 'sw' activity, except for receiving packets (which
121  * the caller must do). */
122 void
123 lswitch_run(struct lswitch *sw)
124 {
125     if (sw->ml) {
126         mac_learning_run(sw->ml, NULL);
127     }
128 }
129
130 void
131 lswitch_wait(struct lswitch *sw)
132 {
133     if (sw->ml) {
134         mac_learning_wait(sw->ml);
135     }
136 }
137
138 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
139  * to the learning switch state in 'sw'.  The most likely result of processing
140  * is that flow-setup and packet-out OpenFlow messages will be sent out on
141  * 'rconn'.  */
142 void
143 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
144                        const struct ofpbuf *msg)
145 {
146     struct processor {
147         uint8_t type;
148         size_t min_size;
149         packet_handler_func *handler;
150     };
151     static const struct processor processors[] = {
152         {
153             OFPT_ECHO_REQUEST,
154             sizeof(struct ofp_header),
155             process_echo_request
156         },
157         {
158             OFPT_FEATURES_REPLY,
159             sizeof(struct ofp_switch_features),
160             process_switch_features
161         },
162         {
163             OFPT_PACKET_IN,
164             offsetof(struct ofp_packet_in, data),
165             process_packet_in
166         },
167         {
168             OFPT_FLOW_REMOVED,
169             sizeof(struct ofp_flow_removed),
170             NULL
171         },
172     };
173     const size_t n_processors = ARRAY_SIZE(processors);
174     const struct processor *p;
175     struct ofp_header *oh;
176
177     oh = msg->data;
178     if (sw->datapath_id == 0
179         && oh->type != OFPT_ECHO_REQUEST
180         && oh->type != OFPT_FEATURES_REPLY) {
181         send_features_request(sw, rconn);
182         return;
183     }
184
185     for (p = processors; p < &processors[n_processors]; p++) {
186         if (oh->type == p->type) {
187             if (msg->size < p->min_size) {
188                 VLOG_WARN_RL(&rl, "%016llx: %s: too short (%zu bytes) for "
189                              "type %"PRIu8" (min %zu)", sw->datapath_id,
190                              rconn_get_name(rconn), msg->size, oh->type,
191                              p->min_size);
192                 return;
193             }
194             if (p->handler) {
195                 (p->handler)(sw, rconn, msg->data);
196             }
197             return;
198         }
199     }
200     if (VLOG_IS_DBG_ENABLED()) {
201         char *s = ofp_to_string(msg->data, msg->size, 2);
202         VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
203                     sw->datapath_id, s);
204         free(s);
205     }
206 }
207 \f
208 static void
209 send_features_request(struct lswitch *sw, struct rconn *rconn)
210 {
211     time_t now = time_now();
212     if (now >= sw->last_features_request + 1) {
213         struct ofpbuf *b;
214         struct ofp_switch_config *osc;
215
216         /* Send OFPT_FEATURES_REQUEST. */
217         make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
218         queue_tx(sw, rconn, b);
219
220         /* Send OFPT_SET_CONFIG. */
221         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
222         osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
223         queue_tx(sw, rconn, b);
224
225         sw->last_features_request = now;
226     }
227 }
228
229 static void
230 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
231 {
232     int retval = rconn_send_with_limit(rconn, b, sw->queued, 10);
233     if (retval && retval != ENOTCONN) {
234         if (retval == EAGAIN) {
235             VLOG_INFO_RL(&rl, "%016llx: %s: tx queue overflow",
236                          sw->datapath_id, rconn_get_name(rconn));
237         } else {
238             VLOG_WARN_RL(&rl, "%016llx: %s: send: %s",
239                          sw->datapath_id, rconn_get_name(rconn),
240                          strerror(retval));
241         }
242     }
243 }
244
245 static void
246 process_switch_features(struct lswitch *sw, struct rconn *rconn OVS_UNUSED,
247                         void *osf_)
248 {
249     struct ofp_switch_features *osf = osf_;
250
251     sw->datapath_id = ntohll(osf->datapath_id);
252 }
253
254 static uint16_t
255 lswitch_choose_destination(struct lswitch *sw, const flow_t *flow)
256 {
257     uint16_t out_port;
258
259     /* Learn the source MAC. */
260     if (sw->ml) {
261         if (mac_learning_learn(sw->ml, flow->dl_src, 0, flow->in_port,
262                                GRAT_ARP_LOCK_NONE)) {
263             VLOG_DBG_RL(&rl, "%016llx: learned that "ETH_ADDR_FMT" is on "
264                         "port %"PRIu16, sw->datapath_id,
265                         ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
266         }
267     }
268
269     /* Drop frames for reserved multicast addresses. */
270     if (eth_addr_is_reserved(flow->dl_dst)) {
271         return OFPP_NONE;
272     }
273
274     out_port = OFPP_FLOOD;
275     if (sw->ml) {
276         int learned_port = mac_learning_lookup(sw->ml, flow->dl_dst, 0, NULL);
277         if (learned_port >= 0) {
278             out_port = learned_port;
279             if (out_port == flow->in_port) {
280                 /* Don't send a packet back out its input port. */
281                 return OFPP_NONE;
282             }
283         }
284     }
285
286     /* Check if we need to use "NORMAL" action. */
287     if (sw->action_normal && out_port != OFPP_FLOOD) {
288         return OFPP_NORMAL;
289     }
290
291     return out_port;
292 }
293
294 static void
295 process_packet_in(struct lswitch *sw, struct rconn *rconn, void *opi_)
296 {
297     struct ofp_packet_in *opi = opi_;
298     uint16_t in_port = ntohs(opi->in_port);
299     uint16_t out_port;
300
301     struct ofp_action_header actions[2];
302     size_t actions_len;
303
304     size_t pkt_ofs, pkt_len;
305     struct ofpbuf pkt;
306     flow_t flow;
307
308     /* Ignore packets sent via output to OFPP_CONTROLLER.  This library never
309      * uses such an action.  You never know what experiments might be going on,
310      * though, and it seems best not to interfere with them. */
311     if (opi->reason != OFPR_NO_MATCH) {
312         return;
313     }
314
315     /* Extract flow data from 'opi' into 'flow'. */
316     pkt_ofs = offsetof(struct ofp_packet_in, data);
317     pkt_len = ntohs(opi->header.length) - pkt_ofs;
318     pkt.data = opi->data;
319     pkt.size = pkt_len;
320     flow_extract(&pkt, 0, in_port, &flow);
321
322     /* Choose output port. */
323     out_port = lswitch_choose_destination(sw, &flow);
324
325     /* Make actions. */
326     if (out_port == OFPP_NONE) {
327         actions_len = 0;
328     } else if (sw->queue == UINT32_MAX || out_port >= OFPP_MAX) {
329         struct ofp_action_output oao;
330
331         memset(&oao, 0, sizeof oao);
332         oao.type = htons(OFPAT_OUTPUT);
333         oao.len = htons(sizeof oao);
334         oao.port = htons(out_port);
335
336         memcpy(actions, &oao, sizeof oao);
337         actions_len = sizeof oao;
338     } else {
339         struct ofp_action_enqueue oae;
340
341         memset(&oae, 0, sizeof oae);
342         oae.type = htons(OFPAT_ENQUEUE);
343         oae.len = htons(sizeof oae);
344         oae.port = htons(out_port);
345         oae.queue_id = htonl(sw->queue);
346
347         memcpy(actions, &oae, sizeof oae);
348         actions_len = sizeof oae;
349     }
350     assert(actions_len <= sizeof actions);
351
352     /* Send the packet, and possibly the whole flow, to the output port. */
353     if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
354         struct ofpbuf *buffer;
355         struct ofp_flow_mod *ofm;
356
357         /* The output port is known, or we always flood everything, so add a
358          * new flow. */
359         buffer = make_add_flow(&flow, ntohl(opi->buffer_id),
360                                sw->max_idle, actions_len);
361         ofpbuf_put(buffer, actions, actions_len);
362         ofm = buffer->data;
363         ofm->match.wildcards = htonl(sw->wildcards);
364         queue_tx(sw, rconn, buffer);
365
366         /* If the switch didn't buffer the packet, we need to send a copy. */
367         if (ntohl(opi->buffer_id) == UINT32_MAX && actions_len > 0) {
368             queue_tx(sw, rconn,
369                      make_packet_out(&pkt, UINT32_MAX, in_port,
370                                      actions, actions_len / sizeof *actions));
371         }
372     } else {
373         /* We don't know that MAC, or we don't set up flows.  Send along the
374          * packet without setting up a flow. */
375         if (ntohl(opi->buffer_id) != UINT32_MAX || actions_len > 0) {
376             queue_tx(sw, rconn,
377                      make_packet_out(&pkt, ntohl(opi->buffer_id), in_port,
378                                      actions, actions_len / sizeof *actions));
379         }
380     }
381 }
382
383 static void
384 process_echo_request(struct lswitch *sw, struct rconn *rconn, void *rq_)
385 {
386     struct ofp_header *rq = rq_;
387     queue_tx(sw, rconn, make_echo_reply(rq));
388 }