1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
35 #include "learning-switch.h"
39 #include <netinet/in.h>
44 #include "mac-learning.h"
46 #include "ofp-print.h"
47 #include "openflow/openflow.h"
48 #include "poll-loop.h"
56 #define THIS_MODULE VLM_learning_switch
63 P_FORWARDING = 1 << 3,
68 /* If nonnegative, the switch sets up flows that expire after the given
69 * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
70 * Otherwise, the switch processes every packet. */
73 unsigned long long int datapath_id;
74 uint32_t capabilities;
75 time_t last_features_request;
76 struct mac_learning *ml; /* NULL to act as hub instead of switch. */
78 /* Number of outgoing queued packets on the rconn. */
81 /* Spanning tree protocol implementation.
83 * We implement STP states by, whenever a port's STP state changes,
84 * querying all the flows on the switch and then deleting any of them that
85 * are inappropriate for a port's STP state. */
86 long long int next_query; /* Next time at which to query all flows. */
87 long long int last_query; /* Last time we sent a query. */
88 long long int last_reply; /* Last time we received a query reply. */
89 unsigned int port_states[STP_MAX_PORTS];
90 uint32_t query_xid; /* XID used for query. */
91 int n_flows, n_no_recv, n_no_send;
94 /* The log messages here could actually be useful in debugging, so keep the
95 * rate limit relatively high. */
96 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
98 static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
99 static void send_features_request(struct lswitch *, struct rconn *);
100 static void schedule_query(struct lswitch *, long long int delay);
101 static bool may_learn(const struct lswitch *, uint16_t port_no);
102 static bool may_recv(const struct lswitch *, uint16_t port_no,
104 static bool may_send(const struct lswitch *, uint16_t port_no);
106 typedef void packet_handler_func(struct lswitch *, struct rconn *, void *);
107 static packet_handler_func process_switch_features;
108 static packet_handler_func process_packet_in;
109 static packet_handler_func process_echo_request;
110 static packet_handler_func process_port_status;
111 static packet_handler_func process_phy_port;
112 static packet_handler_func process_stats_reply;
114 /* Creates and returns a new learning switch.
116 * If 'learn_macs' is true, the new switch will learn the ports on which MAC
117 * addresses appear. Otherwise, the new switch will flood all packets.
119 * If 'max_idle' is nonnegative, the new switch will set up flows that expire
120 * after the given number of seconds (or never expire, if 'max_idle' is
121 * OFP_FLOW_PERMANENT). Otherwise, the new switch will process every packet.
123 * 'rconn' is used to send out an OpenFlow features request. */
125 lswitch_create(struct rconn *rconn, bool learn_macs, int max_idle)
130 sw = xcalloc(1, sizeof *sw);
131 sw->max_idle = max_idle;
133 sw->last_features_request = time_now() - 1;
134 sw->ml = learn_macs ? mac_learning_create() : NULL;
135 sw->next_query = LLONG_MIN;
136 sw->last_query = LLONG_MIN;
137 sw->last_reply = LLONG_MIN;
138 for (i = 0; i < STP_MAX_PORTS; i++) {
139 sw->port_states[i] = P_DISABLED;
141 send_features_request(sw, rconn);
147 lswitch_destroy(struct lswitch *sw)
150 mac_learning_destroy(sw->ml);
155 /* Takes care of necessary 'sw' activity, except for receiving packets (which
156 * the caller must do). */
158 lswitch_run(struct lswitch *sw, struct rconn *rconn)
160 long long int now = time_msec();
162 /* If we're waiting for more replies, keeping waiting for up to 10 s. */
163 if (sw->last_reply != LLONG_MIN) {
164 if (now - sw->last_reply > 10000) {
165 VLOG_ERR_RL(&rl, "%012llx: No more flow stat replies last 10 s",
167 sw->last_reply = LLONG_MIN;
168 sw->last_query = LLONG_MIN;
169 schedule_query(sw, 0);
175 /* If we're waiting for any reply at all, keep waiting for up to 10 s. */
176 if (sw->last_query != LLONG_MIN) {
177 if (now - sw->last_query > 10000) {
178 VLOG_ERR_RL(&rl, "%012llx: No flow stat replies in last 10 s",
180 sw->last_query = LLONG_MIN;
181 schedule_query(sw, 0);
187 /* If it's time to send another query, do so. */
188 if (sw->next_query != LLONG_MIN && now >= sw->next_query) {
189 sw->next_query = LLONG_MIN;
190 if (!rconn_is_connected(rconn)) {
191 schedule_query(sw, 1000);
193 struct ofp_stats_request *osr;
194 struct ofp_flow_stats_request *ofsr;
198 VLOG_DBG("%012llx: Sending flow stats request to implement STP",
201 sw->last_query = now;
202 sw->query_xid = random_uint32();
206 osr = make_openflow_xid(sizeof *osr + sizeof *ofsr,
207 OFPT_STATS_REQUEST, sw->query_xid, &b);
208 osr->type = htons(OFPST_FLOW);
209 osr->flags = htons(0);
210 ofsr = (struct ofp_flow_stats_request *) osr->body;
211 ofsr->match.wildcards = htonl(OFPFW_ALL);
212 ofsr->table_id = 0xff;
213 ofsr->out_port = htons(OFPP_NONE);
215 error = rconn_send(rconn, b, NULL);
217 VLOG_WARN_RL(&rl, "%012llx: sending flow stats request "
218 "failed: %s", sw->datapath_id, strerror(error));
220 schedule_query(sw, 1000);
227 wait_timeout(long long int started)
229 long long int now = time_msec();
230 long long int timeout = 10000 - (now - started);
232 poll_immediate_wake();
234 poll_timer_wait(timeout);
239 lswitch_wait(struct lswitch *sw)
241 if (sw->last_reply != LLONG_MIN) {
242 wait_timeout(sw->last_reply);
243 } else if (sw->last_query != LLONG_MIN) {
244 wait_timeout(sw->last_query);
248 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
249 * to the learning switch state in 'sw'. The most likely result of processing
250 * is that flow-setup and packet-out OpenFlow messages will be sent out on
253 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
254 const struct ofpbuf *msg)
259 packet_handler_func *handler;
261 static const struct processor processors[] = {
264 sizeof(struct ofp_header),
269 sizeof(struct ofp_switch_features),
270 process_switch_features
274 offsetof(struct ofp_packet_in, data),
279 sizeof(struct ofp_port_status),
284 offsetof(struct ofp_stats_reply, body),
289 sizeof(struct ofp_flow_expired),
293 const size_t n_processors = ARRAY_SIZE(processors);
294 const struct processor *p;
295 struct ofp_header *oh;
298 if (sw->datapath_id == 0
299 && oh->type != OFPT_ECHO_REQUEST
300 && oh->type != OFPT_FEATURES_REPLY) {
301 send_features_request(sw, rconn);
305 for (p = processors; p < &processors[n_processors]; p++) {
306 if (oh->type == p->type) {
307 if (msg->size < p->min_size) {
308 VLOG_WARN_RL(&rl, "%012llx: %s: too short (%zu bytes) for "
309 "type %"PRIu8" (min %zu)", sw->datapath_id,
310 rconn_get_name(rconn), msg->size, oh->type,
315 (p->handler)(sw, rconn, msg->data);
320 if (VLOG_IS_DBG_ENABLED()) {
321 char *p = ofp_to_string(msg->data, msg->size, 2);
322 VLOG_DBG_RL(&rl, "%012llx: OpenFlow packet ignored: %s",
329 send_features_request(struct lswitch *sw, struct rconn *rconn)
331 time_t now = time_now();
332 if (now >= sw->last_features_request + 1) {
334 struct ofp_switch_config *osc;
336 /* Send OFPT_FEATURES_REQUEST. */
337 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
338 queue_tx(sw, rconn, b);
340 /* Send OFPT_SET_CONFIG. */
341 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
342 osc->flags = htons(OFPC_SEND_FLOW_EXP);
343 osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
344 queue_tx(sw, rconn, b);
346 sw->last_features_request = now;
351 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
353 int retval = rconn_send_with_limit(rconn, b, &sw->n_queued, 10);
354 if (retval && retval != ENOTCONN) {
355 if (retval == EAGAIN) {
356 VLOG_WARN_RL(&rl, "%012llx: %s: tx queue overflow",
357 sw->datapath_id, rconn_get_name(rconn));
359 VLOG_WARN_RL(&rl, "%012llx: %s: send: %s",
360 sw->datapath_id, rconn_get_name(rconn),
367 schedule_query(struct lswitch *sw, long long int delay)
369 long long int now = time_msec();
370 if (sw->next_query == LLONG_MIN || sw->next_query > now + delay) {
371 sw->next_query = now + delay;
376 process_switch_features(struct lswitch *sw, struct rconn *rconn, void *osf_)
378 struct ofp_switch_features *osf = osf_;
379 size_t n_ports = ((ntohs(osf->header.length)
380 - offsetof(struct ofp_switch_features, ports))
381 / sizeof *osf->ports);
384 sw->datapath_id = ntohll(osf->datapath_id);
385 sw->capabilities = ntohl(osf->capabilities);
386 for (i = 0; i < n_ports; i++) {
387 process_phy_port(sw, rconn, &osf->ports[i]);
389 if (sw->capabilities & OFPC_STP) {
390 schedule_query(sw, 1000);
395 process_packet_in(struct lswitch *sw, struct rconn *rconn, void *opi_)
397 struct ofp_packet_in *opi = opi_;
398 uint16_t in_port = ntohs(opi->in_port);
399 uint16_t out_port = OFPP_FLOOD;
401 size_t pkt_ofs, pkt_len;
405 /* Extract flow data from 'opi' into 'flow'. */
406 pkt_ofs = offsetof(struct ofp_packet_in, data);
407 pkt_len = ntohs(opi->header.length) - pkt_ofs;
408 pkt.data = opi->data;
410 flow_extract(&pkt, in_port, &flow);
412 if (may_learn(sw, in_port) && sw->ml) {
413 if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) {
414 VLOG_DBG_RL(&rl, "%012llx: learned that "ETH_ADDR_FMT" is on "
415 "port %"PRIu16, sw->datapath_id,
416 ETH_ADDR_ARGS(flow.dl_src), in_port);
420 if (eth_addr_is_reserved(flow.dl_src)) {
424 if (!may_recv(sw, in_port, false)) {
425 /* STP prevents receiving anything on this port. */
430 uint16_t learned_port = mac_learning_lookup(sw->ml, flow.dl_dst);
431 if (may_send(sw, learned_port)) {
432 out_port = learned_port;
436 if (in_port == out_port) {
437 /* Don't send out packets on their input ports. */
439 } else if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
440 /* The output port is known, or we always flood everything, so add a
442 queue_tx(sw, rconn, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
443 out_port, sw->max_idle));
445 /* If the switch didn't buffer the packet, we need to send a copy. */
446 if (ntohl(opi->buffer_id) == UINT32_MAX) {
448 make_unbuffered_packet_out(&pkt, in_port, out_port));
451 /* We don't know that MAC, or we don't set up flows. Send along the
452 * packet without setting up a flow. */
454 if (ntohl(opi->buffer_id) == UINT32_MAX) {
455 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
457 b = make_buffered_packet_out(ntohl(opi->buffer_id),
460 queue_tx(sw, rconn, b);
465 if (sw->max_idle >= 0) {
466 /* Set up a flow to drop packets. */
467 queue_tx(sw, rconn, make_add_flow(&flow, ntohl(opi->buffer_id),
470 /* Just drop the packet, since we don't set up flows at all.
471 * XXX we should send a packet_out with no actions if buffer_id !=
472 * UINT32_MAX, to avoid clogging the kernel buffers. */
478 process_echo_request(struct lswitch *sw, struct rconn *rconn, void *rq_)
480 struct ofp_header *rq = rq_;
481 queue_tx(sw, rconn, make_echo_reply(rq));
485 process_port_status(struct lswitch *sw, struct rconn *rconn, void *ops_)
487 struct ofp_port_status *ops = ops_;
488 process_phy_port(sw, rconn, &ops->desc);
492 process_phy_port(struct lswitch *sw, struct rconn *rconn, void *opp_)
494 const struct ofp_phy_port *opp = opp_;
495 uint16_t port_no = ntohs(opp->port_no);
496 if (sw->capabilities & OFPC_STP && port_no < STP_MAX_PORTS) {
497 uint32_t config = ntohl(opp->config);
498 uint32_t state = ntohl(opp->state);
499 unsigned int *port_state = &sw->port_states[port_no];
500 unsigned int new_port_state;
502 if (!(config & (OFPPC_NO_STP | OFPPC_PORT_DOWN))
503 && !(state & OFPPS_LINK_DOWN))
505 switch (state & OFPPS_STP_MASK) {
506 case OFPPS_STP_LISTEN:
507 new_port_state = P_LISTENING;
509 case OFPPS_STP_LEARN:
510 new_port_state = P_LEARNING;
512 case OFPPS_STP_FORWARD:
513 new_port_state = P_FORWARDING;
515 case OFPPS_STP_BLOCK:
516 new_port_state = P_BLOCKING;
519 new_port_state = P_DISABLED;
523 new_port_state = P_FORWARDING;
525 if (*port_state != new_port_state) {
526 *port_state = new_port_state;
527 schedule_query(sw, 1000);
533 get_port_state(const struct lswitch *sw, uint16_t port_no)
535 return (port_no >= STP_MAX_PORTS || !(sw->capabilities & OFPC_STP)
537 : sw->port_states[port_no]);
541 may_learn(const struct lswitch *sw, uint16_t port_no)
543 return get_port_state(sw, port_no) & (P_LEARNING | P_FORWARDING);
547 may_recv(const struct lswitch *sw, uint16_t port_no, bool any_actions)
549 unsigned int state = get_port_state(sw, port_no);
551 ? state & (P_DISABLED | P_LISTENING | P_BLOCKING)
552 : state & (P_DISABLED | P_LISTENING | P_BLOCKING | P_LEARNING));
556 may_send(const struct lswitch *sw, uint16_t port_no)
558 return get_port_state(sw, port_no) & P_FORWARDING;
562 process_flow_stats(struct lswitch *sw, struct rconn *rconn,
563 const struct ofp_flow_stats *ofs)
565 const char *end = (char *) ofs + ntohs(ofs->length);
568 /* Decide to delete the flow if it matches on an STP-disabled physical
569 * port. But don't delete it if the flow just drops all received packets,
570 * because that's a perfectly reasonable thing to do for disabled physical
572 if (!(ofs->match.wildcards & htonl(OFPFW_IN_PORT))) {
573 if (!may_recv(sw, ntohs(ofs->match.in_port),
574 end > (char *) ofs->actions)) {
580 /* Decide to delete the flow if it forwards to an STP-disabled physical
583 const struct ofp_action_header *a;
586 for (a = ofs->actions; (char *) a < end; a += len / 8) {
590 if (len > end - (char *) a) {
591 VLOG_DBG_RL(&rl, "%012llx: action exceeds available space "
593 sw->datapath_id, len, end - (char *) a);
595 } else if (len % 8) {
596 VLOG_DBG_RL(&rl, "%012llx: action length (%zu) not multiple "
597 "of 8 bytes", sw->datapath_id, len);
601 type = ntohs(a->type);
602 if (a->type == htons(OFPAT_OUTPUT)) {
603 struct ofp_action_output *oao = (struct ofp_action_output *) a;
604 if (!may_send(sw, ntohs(oao->port))) {
613 /* Delete the flow. */
615 struct ofp_flow_mod *ofm;
618 ofm = make_openflow(offsetof(struct ofp_flow_mod, actions),
620 ofm->match = ofs->match;
621 ofm->command = OFPFC_DELETE_STRICT;
622 rconn_send(rconn, b, NULL);
627 process_stats_reply(struct lswitch *sw, struct rconn *rconn, void *osr_)
629 struct ofp_stats_reply *osr = osr_;
630 const uint8_t *body = osr->body;
631 const uint8_t *pos = body;
634 if (sw->last_query == LLONG_MIN
635 || osr->type != htons(OFPST_FLOW)
636 || osr->header.xid != sw->query_xid) {
639 body_len = (ntohs(osr->header.length)
640 - offsetof(struct ofp_stats_reply, body));
642 const struct ofp_flow_stats *fs;
643 ptrdiff_t bytes_left = body + body_len - pos;
646 if (bytes_left < sizeof *fs) {
647 if (bytes_left != 0) {
648 VLOG_WARN_RL(&rl, "%012llx: %td leftover bytes in flow "
649 "stats reply", sw->datapath_id, bytes_left);
654 fs = (const void *) pos;
655 length = ntohs(fs->length);
656 if (length < sizeof *fs) {
657 VLOG_WARN_RL(&rl, "%012llx: flow stats length %zu is shorter than "
658 "min %zu", sw->datapath_id, length, sizeof *fs);
660 } else if (length > bytes_left) {
661 VLOG_WARN_RL(&rl, "%012llx: flow stats length %zu but only %td "
662 "bytes left", sw->datapath_id, length, bytes_left);
664 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
665 VLOG_WARN_RL(&rl, "%012llx: flow stats length %zu has %zu bytes "
666 "left over in final action", sw->datapath_id, length,
667 (length - sizeof *fs) % sizeof fs->actions[0]);
672 process_flow_stats(sw, rconn, fs);
676 if (!(osr->flags & htons(OFPSF_REPLY_MORE))) {
677 VLOG_DBG("%012llx: Deleted %d of %d received flows to "
678 "implement STP, %d because of no-recv, %d because of "
679 "no-send", sw->datapath_id,
680 sw->n_no_recv + sw->n_no_send, sw->n_flows,
681 sw->n_no_recv, sw->n_no_send);
682 sw->last_query = LLONG_MIN;
683 sw->last_reply = LLONG_MIN;
685 sw->last_reply = time_msec();