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"
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;
214 error = rconn_send(rconn, b, NULL);
216 VLOG_WARN_RL(&rl, "%012llx: sending flow stats request "
217 "failed: %s", sw->datapath_id, strerror(error));
219 schedule_query(sw, 1000);
226 wait_timeout(long long int started)
228 long long int now = time_msec();
229 long long int timeout = 10000 - (now - started);
231 poll_immediate_wake();
233 poll_timer_wait(timeout);
238 lswitch_wait(struct lswitch *sw)
240 if (sw->last_reply != LLONG_MIN) {
241 wait_timeout(sw->last_reply);
242 } else if (sw->last_query != LLONG_MIN) {
243 wait_timeout(sw->last_query);
247 /* Processes 'msg', which should be an OpenFlow received on 'rconn', according
248 * to the learning switch state in 'sw'. The most likely result of processing
249 * is that flow-setup and packet-out OpenFlow messages will be sent out on
252 lswitch_process_packet(struct lswitch *sw, struct rconn *rconn,
253 const struct ofpbuf *msg)
258 packet_handler_func *handler;
260 static const struct processor processors[] = {
263 sizeof(struct ofp_header),
268 sizeof(struct ofp_switch_features),
269 process_switch_features
273 offsetof(struct ofp_packet_in, data),
278 sizeof(struct ofp_port_status),
283 offsetof(struct ofp_stats_reply, body),
288 sizeof(struct ofp_flow_expired),
292 const size_t n_processors = ARRAY_SIZE(processors);
293 const struct processor *p;
294 struct ofp_header *oh;
297 if (sw->datapath_id == 0
298 && oh->type != OFPT_ECHO_REQUEST
299 && oh->type != OFPT_FEATURES_REPLY) {
300 send_features_request(sw, rconn);
304 for (p = processors; p < &processors[n_processors]; p++) {
305 if (oh->type == p->type) {
306 if (msg->size < p->min_size) {
307 VLOG_WARN_RL(&rl, "%012llx: %s: too short (%zu bytes) for "
308 "type %"PRIu8" (min %zu)", sw->datapath_id,
309 rconn_get_name(rconn), msg->size, oh->type,
314 (p->handler)(sw, rconn, msg->data);
319 if (VLOG_IS_DBG_ENABLED()) {
320 char *p = ofp_to_string(msg->data, msg->size, 2);
321 VLOG_DBG_RL(&rl, "%012llx: OpenFlow packet ignored: %s",
328 send_features_request(struct lswitch *sw, struct rconn *rconn)
330 time_t now = time_now();
331 if (now >= sw->last_features_request + 1) {
333 struct ofp_switch_config *osc;
335 /* Send OFPT_FEATURES_REQUEST. */
336 make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &b);
337 queue_tx(sw, rconn, b);
339 /* Send OFPT_SET_CONFIG. */
340 osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &b);
341 osc->flags = htons(OFPC_SEND_FLOW_EXP);
342 osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
343 queue_tx(sw, rconn, b);
345 sw->last_features_request = now;
350 queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
352 int retval = rconn_send_with_limit(rconn, b, &sw->n_queued, 10);
353 if (retval && retval != ENOTCONN) {
354 if (retval == EAGAIN) {
355 VLOG_WARN_RL(&rl, "%012llx: %s: tx queue overflow",
356 sw->datapath_id, rconn_get_name(rconn));
358 VLOG_WARN_RL(&rl, "%012llx: %s: send: %s",
359 sw->datapath_id, rconn_get_name(rconn),
366 schedule_query(struct lswitch *sw, long long int delay)
368 long long int now = time_msec();
369 if (sw->next_query == LLONG_MIN || sw->next_query > now + delay) {
370 sw->next_query = now + delay;
375 process_switch_features(struct lswitch *sw, struct rconn *rconn, void *osf_)
377 struct ofp_switch_features *osf = osf_;
378 size_t n_ports = ((ntohs(osf->header.length)
379 - offsetof(struct ofp_switch_features, ports))
380 / sizeof *osf->ports);
383 sw->datapath_id = ntohll(osf->datapath_id);
384 sw->capabilities = ntohl(osf->capabilities);
385 for (i = 0; i < n_ports; i++) {
386 process_phy_port(sw, rconn, &osf->ports[i]);
388 if (sw->capabilities & OFPC_STP) {
389 schedule_query(sw, 1000);
394 process_packet_in(struct lswitch *sw, struct rconn *rconn, void *opi_)
396 struct ofp_packet_in *opi = opi_;
397 uint16_t in_port = ntohs(opi->in_port);
398 uint16_t out_port = OFPP_FLOOD;
400 size_t pkt_ofs, pkt_len;
404 /* Extract flow data from 'opi' into 'flow'. */
405 pkt_ofs = offsetof(struct ofp_packet_in, data);
406 pkt_len = ntohs(opi->header.length) - pkt_ofs;
407 pkt.data = opi->data;
409 flow_extract(&pkt, in_port, &flow);
411 if (may_learn(sw, in_port) && sw->ml) {
412 if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) {
413 VLOG_DBG_RL(&rl, "%012llx: learned that "ETH_ADDR_FMT" is on "
414 "port %"PRIu16, sw->datapath_id,
415 ETH_ADDR_ARGS(flow.dl_src), in_port);
419 if (!may_recv(sw, in_port, false)) {
420 /* STP prevents receiving anything on this port. */
425 uint16_t learned_port = mac_learning_lookup(sw->ml, flow.dl_dst);
426 if (may_send(sw, learned_port)) {
427 out_port = learned_port;
431 if (in_port == out_port) {
432 /* Don't send out packets on their input ports. */
434 } else if (sw->max_idle >= 0 && (!sw->ml || out_port != OFPP_FLOOD)) {
435 /* The output port is known, or we always flood everything, so add a
437 queue_tx(sw, rconn, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
438 out_port, sw->max_idle));
440 /* If the switch didn't buffer the packet, we need to send a copy. */
441 if (ntohl(opi->buffer_id) == UINT32_MAX) {
443 make_unbuffered_packet_out(&pkt, in_port, out_port));
446 /* We don't know that MAC, or we don't set up flows. Send along the
447 * packet without setting up a flow. */
449 if (ntohl(opi->buffer_id) == UINT32_MAX) {
450 b = make_unbuffered_packet_out(&pkt, in_port, out_port);
452 b = make_buffered_packet_out(ntohl(opi->buffer_id),
455 queue_tx(sw, rconn, b);
460 /* Set up a flow to drop packets, or just drop the packet if we don't set
461 * up flows at all. */
462 if (sw->max_idle >= 0) {
463 queue_tx(sw, rconn, make_add_flow(&flow, ntohl(opi->buffer_id),
470 process_echo_request(struct lswitch *sw, struct rconn *rconn, void *rq_)
472 struct ofp_header *rq = rq_;
473 queue_tx(sw, rconn, make_echo_reply(rq));
477 process_port_status(struct lswitch *sw, struct rconn *rconn, void *ops_)
479 struct ofp_port_status *ops = ops_;
480 process_phy_port(sw, rconn, &ops->desc);
484 process_phy_port(struct lswitch *sw, struct rconn *rconn, void *opp_)
486 const struct ofp_phy_port *opp = opp_;
487 uint16_t port_no = ntohs(opp->port_no);
488 if (sw->capabilities & OFPC_STP && port_no < STP_MAX_PORTS) {
489 uint32_t config = ntohl(opp->config);
490 uint32_t state = ntohl(opp->state);
491 unsigned int *port_state = &sw->port_states[port_no];
492 unsigned int new_port_state;
494 if (!(config & (OFPPC_NO_STP | OFPPC_PORT_DOWN))
495 && !(state & OFPPS_LINK_DOWN))
497 switch (state & OFPPS_STP_MASK) {
498 case OFPPS_STP_LISTEN:
499 new_port_state = P_LISTENING;
501 case OFPPS_STP_LEARN:
502 new_port_state = P_LEARNING;
504 case OFPPS_STP_FORWARD:
505 new_port_state = P_FORWARDING;
507 case OFPPS_STP_BLOCK:
508 new_port_state = P_BLOCKING;
511 new_port_state = P_DISABLED;
515 new_port_state = P_FORWARDING;
517 if (*port_state != new_port_state) {
518 *port_state = new_port_state;
519 schedule_query(sw, 1000);
525 get_port_state(const struct lswitch *sw, uint16_t port_no)
527 return (port_no >= STP_MAX_PORTS || !(sw->capabilities & OFPC_STP)
529 : sw->port_states[port_no]);
533 may_learn(const struct lswitch *sw, uint16_t port_no)
535 return get_port_state(sw, port_no) & (P_LEARNING | P_FORWARDING);
539 may_recv(const struct lswitch *sw, uint16_t port_no, bool any_actions)
541 unsigned int state = get_port_state(sw, port_no);
543 ? state & (P_DISABLED | P_LISTENING | P_BLOCKING)
544 : state & (P_DISABLED | P_LISTENING | P_BLOCKING | P_LEARNING));
548 may_send(const struct lswitch *sw, uint16_t port_no)
550 return get_port_state(sw, port_no) & P_FORWARDING;
554 process_flow_stats(struct lswitch *sw, struct rconn *rconn,
555 const struct ofp_flow_stats *ofs)
557 const char *end = (char *) ofs + ntohs(ofs->length);
560 /* Decide to delete the flow if it matches on an STP-disabled physical
561 * port. But don't delete it if the flow just drops all received packets,
562 * because that's a perfectly reasonable thing to do for disabled physical
564 if (!(ofs->match.wildcards & htonl(OFPFW_IN_PORT))) {
565 if (!may_recv(sw, ntohs(ofs->match.in_port),
566 end > (char *) ofs->actions)) {
572 /* Decide to delete the flow if it forwards to an STP-disabled physical
575 const struct ofp_action_header *a;
578 for (a = ofs->actions; (char *) a < end; a += len / 8) {
582 if (len > end - (char *) a) {
583 VLOG_DBG_RL(&rl, "%012llx: action exceeds available space "
585 sw->datapath_id, len, end - (char *) a);
587 } else if (len % 8) {
588 VLOG_DBG_RL(&rl, "%012llx: action length (%zu) not multiple "
589 "of 8 bytes", sw->datapath_id, len);
593 type = ntohs(a->type);
594 if (a->type == htons(OFPAT_OUTPUT)) {
595 struct ofp_action_output *oao = (struct ofp_action_output *) a;
596 if (!may_send(sw, ntohs(oao->port))) {
605 /* Delete the flow. */
607 struct ofp_flow_mod *ofm;
610 ofm = make_openflow(offsetof(struct ofp_flow_mod, actions),
612 ofm->match = ofs->match;
613 ofm->command = OFPFC_DELETE_STRICT;
614 rconn_send(rconn, b, NULL);
619 process_stats_reply(struct lswitch *sw, struct rconn *rconn, void *osr_)
621 struct ofp_stats_reply *osr = osr_;
622 const uint8_t *body = osr->body;
623 const uint8_t *pos = body;
626 if (sw->last_query == LLONG_MIN
627 || osr->type != htons(OFPST_FLOW)
628 || osr->header.xid != sw->query_xid) {
631 body_len = (ntohs(osr->header.length)
632 - offsetof(struct ofp_stats_reply, body));
634 const struct ofp_flow_stats *fs;
635 ptrdiff_t bytes_left = body + body_len - pos;
638 if (bytes_left < sizeof *fs) {
639 if (bytes_left != 0) {
640 VLOG_WARN_RL(&rl, "%012llx: %td leftover bytes in flow "
641 "stats reply", sw->datapath_id, bytes_left);
646 fs = (const void *) pos;
647 length = ntohs(fs->length);
648 if (length < sizeof *fs) {
649 VLOG_WARN_RL(&rl, "%012llx: flow stats length %zu is shorter than "
650 "min %zu", sw->datapath_id, length, sizeof *fs);
652 } else if (length > bytes_left) {
653 VLOG_WARN_RL(&rl, "%012llx: flow stats length %zu but only %td "
654 "bytes left", sw->datapath_id, length, bytes_left);
656 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
657 VLOG_WARN_RL(&rl, "%012llx: flow stats length %zu has %zu bytes "
658 "left over in final action", sw->datapath_id, length,
659 (length - sizeof *fs) % sizeof fs->actions[0]);
664 process_flow_stats(sw, rconn, fs);
668 if (!(osr->flags & htons(OFPSF_REPLY_MORE))) {
669 VLOG_DBG("%012llx: Deleted %d of %d received flows to "
670 "implement STP, %d because of no-recv, %d because of "
671 "no-send", sw->datapath_id,
672 sw->n_no_recv + sw->n_no_send, sw->n_flows,
673 sw->n_no_recv, sw->n_no_send);
674 sw->last_query = LLONG_MIN;
675 sw->last_reply = LLONG_MIN;
677 sw->last_reply = time_msec();