From: Ben Pfaff Date: Wed, 4 Jun 2008 18:08:08 +0000 (-0700) Subject: Factor learning switch out of controller into library. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b351ea0762fc73e1e92059e5f16b8269db44bd2;p=openvswitch Factor learning switch out of controller into library. This prepares for adding "fail-open" support to the secchan, which will require the secchan to act as a learning switch. --- diff --git a/controller/controller.c b/controller/controller.c index 3a29043d..0cc42589 100644 --- a/controller/controller.c +++ b/controller/controller.c @@ -31,51 +31,32 @@ * derivatives without specific, written prior permission. */ -#include #include #include -#include -#include -#include #include #include -#include #include "buffer.h" #include "command-line.h" #include "compiler.h" #include "fault.h" -#include "flow.h" -#include "hash.h" -#include "list.h" -#include "ofp-print.h" -#include "mac-learning.h" -#include "openflow.h" -#include "packets.h" +#include "learning-switch.h" #include "poll-loop.h" -#include "queue.h" -#include "time.h" +#include "rconn.h" #include "util.h" #include "vconn-ssl.h" #include "vconn.h" #include "vlog-socket.h" -#include "xtoxll.h" #include "vlog.h" #define THIS_MODULE VLM_controller #define MAX_SWITCHES 16 -#define MAX_TXQ 128 +#define MAX_LISTENERS 16 struct switch_ { - char *name; - struct vconn *vconn; - - uint64_t datapath_id; - time_t last_features_request; - - struct queue txq; - struct mac_learning *ml; + struct lswitch *lswitch; + struct rconn *rconn; }; /* Learn the ports on which MAC addresses appear? */ @@ -84,28 +65,17 @@ static bool learn_macs = true; /* Set up flows? (If not, every packet is processed at the controller.) */ static bool setup_flows = true; +static int do_switching(struct switch_ *); +static void new_switch(struct switch_ *, struct vconn *, const char *name); static void parse_options(int argc, char *argv[]); static void usage(void) NO_RETURN; -static struct switch_ *connect_switch(const char *name); -static struct switch_ *new_switch(const char *name, struct vconn *); -static void close_switch(struct switch_ *); - -static void queue_tx(struct switch_ *, struct buffer *); - -static void send_features_request(struct switch_ *); - -static int do_switch_recv(struct switch_ *this); -static int do_switch_send(struct switch_ *this); - -static void process_packet(struct switch_ *, struct buffer *); -static void process_packet_in(struct switch_ *, struct ofp_packet_in *); - int main(int argc, char *argv[]) { - struct switch_ *switches[MAX_SWITCHES]; - int n_switches; + struct switch_ switches[MAX_SWITCHES]; + struct vconn *listeners[MAX_LISTENERS]; + int n_switches, n_listeners; int retval; int i; @@ -123,58 +93,71 @@ main(int argc, char *argv[]) fatal(retval, "Could not listen for vlog connections"); } - n_switches = 0; - for (i = 0; i < argc - optind; i++) { - struct switch_ *this = connect_switch(argv[optind + i]); - if (this) { + n_switches = n_listeners = 0; + for (i = optind; i < argc; i++) { + const char *name = argv[i]; + struct vconn *vconn; + int retval; + + retval = vconn_open(name, &vconn); + if (retval) { + VLOG_ERR("%s: connect: %s", name, strerror(retval)); + continue; + } + + if (vconn_is_passive(vconn)) { + if (n_listeners >= MAX_LISTENERS) { + fatal(0, "max %d passive connections", n_listeners); + } + listeners[n_listeners++] = vconn; + } else { if (n_switches >= MAX_SWITCHES) { fatal(0, "max %d switch connections", n_switches); } - switches[n_switches++] = this; + new_switch(&switches[n_switches++], vconn, name); } } - if (n_switches == 0) { - fatal(0, "could not connect to any switches"); + if (n_switches == 0 && n_listeners == 0) { + fatal(0, "no active or passive switch connections"); } - while (n_switches > 0) { - /* Do some work. Limit the number of iterations so that callbacks - * registered with the poll loop don't starve. */ + while (n_switches > 0 || n_listeners > 0) { int iteration; int i; + + /* Accept connections on listening vconns. */ + for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) { + struct vconn *new_vconn; + int retval; + + retval = vconn_accept(listeners[i], &new_vconn); + if (!retval || retval == EAGAIN) { + if (!retval) { + new_switch(&switches[n_switches++], new_vconn, "tcp"); + } + i++; + } else { + vconn_close(listeners[i]); + listeners[i] = listeners[--n_listeners]; + } + } + + /* Do some switching work. Limit the number of iterations so that + * callbacks registered with the poll loop don't starve. */ for (iteration = 0; iteration < 50; iteration++) { bool progress = false; for (i = 0; i < n_switches; ) { - struct switch_ *this = switches[i]; - int retval; - - if (vconn_is_passive(this->vconn)) { - retval = 0; - while (n_switches < MAX_SWITCHES) { - struct vconn *new_vconn; - retval = vconn_accept(this->vconn, &new_vconn); - if (retval) { - break; - } - switches[n_switches++] = new_switch("tcp", new_vconn); + struct switch_ *this = &switches[i]; + int retval = do_switching(this); + if (!retval || retval == EAGAIN) { + if (!retval) { + progress = true; } + i++; } else { - retval = do_switch_recv(this); - if (!retval || retval == EAGAIN) { - do { - retval = do_switch_send(this); - if (!retval) { - progress = true; - } - } while (!retval); - } - } - - if (retval && retval != EAGAIN) { - close_switch(this); + lswitch_destroy(this->lswitch); + rconn_destroy(this->rconn); switches[i] = switches[--n_switches]; - } else { - i++; } } if (!progress) { @@ -183,226 +166,47 @@ main(int argc, char *argv[]) } /* Wait for something to happen. */ - for (i = 0; i < n_switches; i++) { - struct switch_ *this = switches[i]; - if (vconn_is_passive(this->vconn)) { - if (n_switches < MAX_SWITCHES) { - vconn_accept_wait(this->vconn); - } - } else { - vconn_recv_wait(this->vconn); - if (this->txq.n) { - vconn_send_wait(this->vconn); - } + if (n_switches < MAX_SWITCHES) { + for (i = 0; i < n_listeners; i++) { + vconn_accept_wait(listeners[i]); } } + for (i = 0; i < n_switches; i++) { + struct switch_ *sw = &switches[i]; + rconn_run_wait(sw->rconn); + rconn_recv_wait(sw->rconn); + } poll_block(); } return 0; } -static int -do_switch_recv(struct switch_ *this) -{ - struct buffer *msg; - int retval; - - retval = vconn_recv(this->vconn, &msg); - if (!retval) { - process_packet(this, msg); - buffer_delete(msg); - } - return retval; -} - -static int -do_switch_send(struct switch_ *this) -{ - int retval = 0; - if (this->txq.n) { - struct buffer *next = this->txq.head->next; - retval = vconn_send(this->vconn, this->txq.head); - if (retval) { - return retval; - } - queue_advance_head(&this->txq, next); - return 0; - } - return EAGAIN; -} - -struct switch_ * -connect_switch(const char *name) -{ - struct vconn *vconn; - int retval; - - retval = vconn_open(name, &vconn); - if (retval) { - VLOG_ERR("%s: connect: %s", name, strerror(retval)); - return NULL; - } - - return new_switch(name, vconn); -} - -static struct switch_ * -new_switch(const char *name, struct vconn *vconn) -{ - struct switch_ *this = xmalloc(sizeof *this); - memset(this, 0, sizeof *this); - this->name = xstrdup(name); - this->vconn = vconn; - queue_init(&this->txq); - this->last_features_request = 0; - if (!vconn_is_passive(vconn)) { - send_features_request(this); - } - if (learn_macs) { - this->ml = mac_learning_create(); - } - return this; -} - static void -close_switch(struct switch_ *this) +new_switch(struct switch_ *sw, struct vconn *vconn, const char *name) { - if (this) { - free(this->name); - vconn_close(this->vconn); - queue_destroy(&this->txq); - mac_learning_destroy(this->ml); - free(this); - } + sw->rconn = rconn_new_from_vconn(name, 128, vconn); + sw->lswitch = lswitch_create(sw->rconn, learn_macs, setup_flows); } -static void -send_features_request(struct switch_ *this) -{ - time_t now = time(0); - if (now >= this->last_features_request + 1) { - struct buffer *b; - struct ofp_header *ofr; - struct ofp_switch_config *osc; - - /* Send OFPT_FEATURES_REQUEST. */ - b = buffer_new(0); - ofr = buffer_put_uninit(b, sizeof *ofr); - memset(ofr, 0, sizeof *ofr); - ofr->type = OFPT_FEATURES_REQUEST; - ofr->version = OFP_VERSION; - ofr->length = htons(sizeof *ofr); - queue_tx(this, b); - - /* Send OFPT_SET_CONFIG. */ - b = buffer_new(0); - osc = buffer_put_uninit(b, sizeof *osc); - memset(osc, 0, sizeof *osc); - osc->header.type = OFPT_SET_CONFIG; - osc->header.version = OFP_VERSION; - osc->header.length = htons(sizeof *osc); - osc->flags = htons(OFPC_SEND_FLOW_EXP); - osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN); - queue_tx(this, b); - - this->last_features_request = now; - } -} - -static void -queue_tx(struct switch_ *this, struct buffer *b) -{ - queue_push_tail(&this->txq, b); -} - -static void -process_packet(struct switch_ *sw, struct buffer *msg) +static int +do_switching(struct switch_ *sw) { - static const size_t min_size[UINT8_MAX + 1] = { - [0 ... UINT8_MAX] = sizeof (struct ofp_header), - [OFPT_FEATURES_REPLY] = sizeof (struct ofp_switch_features), - [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data), - }; - struct ofp_header *oh; - - oh = msg->data; - if (msg->size < min_size[oh->type]) { - VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)", - sw->name, msg->size, oh->type, min_size[oh->type]); - return; - } + unsigned int packets_sent; + struct buffer *msg; - if (oh->type == OFPT_FEATURES_REPLY) { - struct ofp_switch_features *osf = msg->data; - sw->datapath_id = osf->datapath_id; - } else if (sw->datapath_id == 0) { - send_features_request(sw); - } else if (oh->type == OFPT_PACKET_IN) { - struct ofp_packet_in *opi = msg->data; - if (sw->txq.n >= MAX_TXQ) { - /* FIXME: ratelimit. */ - VLOG_WARN("%s: tx queue overflow", sw->name); - } else { - process_packet_in(sw, opi); - } - } else { - if (VLOG_IS_DBG_ENABLED()) { - char *p = ofp_to_string(msg->data, msg->size, 2); - VLOG_DBG("OpenFlow packet ignored: %s", p); - free(p); - } - } -} + packets_sent = rconn_packets_sent(sw->rconn); -static void -process_packet_in(struct switch_ *sw, struct ofp_packet_in *opi) -{ - uint16_t in_port = ntohs(opi->in_port); - uint16_t out_port = OFPP_FLOOD; - - size_t pkt_ofs, pkt_len; - struct buffer pkt; - struct flow flow; - - /* Extract flow data from 'opi' into 'flow'. */ - pkt_ofs = offsetof(struct ofp_packet_in, data); - pkt_len = ntohs(opi->header.length) - pkt_ofs; - pkt.data = opi->data; - pkt.size = pkt_len; - flow_extract(&pkt, in_port, &flow); - - if (learn_macs) { - if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) { - VLOG_DBG("learned that "ETH_ADDR_FMT" is on datapath %" - PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src), - ntohll(sw->datapath_id), in_port); - } - out_port = mac_learning_lookup(sw->ml, flow.dl_dst); + msg = rconn_recv(sw->rconn); + if (msg) { + lswitch_process_packet(sw->lswitch, sw->rconn, msg); + buffer_delete(msg); } + rconn_run(sw->rconn); - if (setup_flows && (!learn_macs || out_port != OFPP_FLOOD)) { - /* The output port is known, or we always flood everything, so add a - * new flow. */ - queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id), - out_port)); - - /* If the switch didn't buffer the packet, we need to send a copy. */ - if (ntohl(opi->buffer_id) == UINT32_MAX) { - queue_tx(sw, make_unbuffered_packet_out(&pkt, in_port, out_port)); - } - } else { - /* We don't know that MAC, or we don't set up flows. Send along the - * packet without setting up a flow. */ - struct buffer *b; - if (ntohl(opi->buffer_id) == UINT32_MAX) { - b = make_unbuffered_packet_out(&pkt, in_port, out_port); - } else { - b = make_buffered_packet_out(ntohl(opi->buffer_id), - in_port, out_port); - } - queue_tx(sw, b); - } + return (!rconn_is_alive(sw->rconn) ? EOF + : rconn_packets_sent(sw->rconn) != packets_sent ? 0 + : EAGAIN); } static void diff --git a/include/Makefile.am b/include/Makefile.am index 848acc08..f6ee667d 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -8,6 +8,7 @@ noinst_HEADERS = \ fault.h \ flow.h \ hash.h \ + learning-switch.h \ list.h \ mac-learning.h \ netdev.h \ diff --git a/include/learning-switch.h b/include/learning-switch.h new file mode 100644 index 00000000..8e304461 --- /dev/null +++ b/include/learning-switch.h @@ -0,0 +1,48 @@ +/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford + * Junior University + * + * We are making the OpenFlow specification and associated documentation + * (Software) available for public use and benefit with the expectation + * that others will use, modify and enhance the Software and contribute + * those enhancements back to the community. However, since we would + * like to make the Software available for broadest use, with as few + * restrictions as possible permission is hereby granted, free of + * charge, to any person obtaining a copy of this Software to deal in + * the Software under the copyrights without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * The name and trademarks of copyright holder(s) may NOT be used in + * advertising or publicity pertaining to the Software or any + * derivatives without specific, written prior permission. + */ + +#ifndef LEARNING_SWITCH_H +#define LEARNING_SWITCH_H 1 + +#include + +struct buffer; +struct rconn; + +struct lswitch *lswitch_create(struct rconn *, + bool learn_macs, bool setup_flows); +void lswitch_destroy(struct lswitch *); +void lswitch_process_packet(struct lswitch *, struct rconn *, + const struct buffer *); + +#endif /* learning-switch.h */ diff --git a/include/rconn.h b/include/rconn.h index 14889bd1..af476cbd 100644 --- a/include/rconn.h +++ b/include/rconn.h @@ -64,9 +64,11 @@ void rconn_recv_wait(struct rconn *); int rconn_send(struct rconn *, struct buffer *); int rconn_force_send(struct rconn *, struct buffer *); bool rconn_is_full(const struct rconn *); +unsigned int rconn_packets_sent(const struct rconn *); const char *rconn_get_name(const struct rconn *); bool rconn_is_alive(const struct rconn *); bool rconn_is_connected(const struct rconn *); +int rconn_disconnected_duration(const struct rconn *); #endif /* rconn.h */ diff --git a/include/vlog.h b/include/vlog.h index 58d3d5ab..2e389f7b 100644 --- a/include/vlog.h +++ b/include/vlog.h @@ -69,6 +69,7 @@ enum vlog_facility vlog_get_facility_val(const char *name); VLOG_MODULE(dpctl) \ VLOG_MODULE(fault) \ VLOG_MODULE(flow) \ + VLOG_MODULE(learning_switch) \ VLOG_MODULE(mac_learning) \ VLOG_MODULE(netdev) \ VLOG_MODULE(netlink) \ diff --git a/lib/Makefile.am b/lib/Makefile.am index 3f594390..0ffc3fdc 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -10,6 +10,7 @@ libopenflow_la_SOURCES = \ fault.c \ flow.c \ hash.c \ + learning-switch.c \ list.c \ mac-learning.c \ netdev.c \ diff --git a/lib/learning-switch.c b/lib/learning-switch.c new file mode 100644 index 00000000..5768fd4d --- /dev/null +++ b/lib/learning-switch.c @@ -0,0 +1,239 @@ +/* Copyright (c) 2008 The Board of Trustees of The Leland Stanford + * Junior University + * + * We are making the OpenFlow specification and associated documentation + * (Software) available for public use and benefit with the expectation + * that others will use, modify and enhance the Software and contribute + * those enhancements back to the community. However, since we would + * like to make the Software available for broadest use, with as few + * restrictions as possible permission is hereby granted, free of + * charge, to any person obtaining a copy of this Software to deal in + * the Software under the copyrights without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * The name and trademarks of copyright holder(s) may NOT be used in + * advertising or publicity pertaining to the Software or any + * derivatives without specific, written prior permission. + */ + +#include "learning-switch.h" + +#include +#include +#include +#include +#include + +#include "buffer.h" +#include "flow.h" +#include "mac-learning.h" +#include "ofp-print.h" +#include "openflow.h" +#include "queue.h" +#include "rconn.h" +#include "vconn.h" +#include "xtoxll.h" + +#define THIS_MODULE VLM_learning_switch +#include "vlog.h" + +struct lswitch { + bool setup_flows; /* Set up flows? (or controller processes all packets) */ + uint64_t datapath_id; + time_t last_features_request; + struct mac_learning *ml; /* NULL to act as hub instead of switch. */ +}; + +static void queue_tx(struct lswitch *, struct rconn *, struct buffer *); +static void send_features_request(struct lswitch *, struct rconn *); +static void process_packet_in(struct lswitch *, struct rconn *, + struct ofp_packet_in *); + +/* Creates and returns a new learning switch. + * + * If 'learn_macs' is true, the new switch will learn the ports on which MAC + * addresses appear. Otherwise, the new switch will flood all packets. + * + * If 'setup_flows' is true, the new switch will set up flows. Otherwise, the + * new switch will process every packet. + * + * 'rconn' is used to send out an OpenFlow features request. */ +struct lswitch * +lswitch_create(struct rconn *rconn, bool learn_macs, bool setup_flows) +{ + struct lswitch *sw = xmalloc(sizeof *sw); + memset(sw, 0, sizeof *sw); + sw->setup_flows = setup_flows; + sw->datapath_id = 0; + sw->last_features_request = 0; + sw->ml = learn_macs ? mac_learning_create() : NULL; + send_features_request(sw, rconn); + return sw; +} + +/* Destroys 'sw'. */ +void +lswitch_destroy(struct lswitch *sw) +{ + if (sw) { + mac_learning_destroy(sw->ml); + free(sw); + } +} + +/* Processes 'msg', which should be an OpenFlow received on 'rconn', according + * to the learning switch state in 'sw'. The most likely result of processing + * is that flow-setup and packet-out OpenFlow messages will be sent out on + * 'rconn'. */ +void +lswitch_process_packet(struct lswitch *sw, struct rconn *rconn, + const struct buffer *msg) +{ + static const size_t min_size[UINT8_MAX + 1] = { + [0 ... UINT8_MAX] = sizeof (struct ofp_header), + [OFPT_FEATURES_REPLY] = sizeof (struct ofp_switch_features), + [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data), + }; + struct ofp_header *oh; + + oh = msg->data; + if (msg->size < min_size[oh->type]) { + VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)", + rconn_get_name(rconn), + msg->size, oh->type, min_size[oh->type]); + return; + } + + if (oh->type == OFPT_FEATURES_REPLY) { + struct ofp_switch_features *osf = msg->data; + sw->datapath_id = osf->datapath_id; + } else if (sw->datapath_id == 0) { + send_features_request(sw, rconn); + } else if (oh->type == OFPT_PACKET_IN) { + process_packet_in(sw, rconn, msg->data); + } else { + if (VLOG_IS_DBG_ENABLED()) { + char *p = ofp_to_string(msg->data, msg->size, 2); + VLOG_DBG("OpenFlow packet ignored: %s", p); + free(p); + } + } +} + +static void +send_features_request(struct lswitch *sw, struct rconn *rconn) +{ + time_t now = time(0); + if (now >= sw->last_features_request + 1) { + struct buffer *b; + struct ofp_header *ofr; + struct ofp_switch_config *osc; + + /* Send OFPT_FEATURES_REQUEST. */ + b = buffer_new(0); + ofr = buffer_put_uninit(b, sizeof *ofr); + memset(ofr, 0, sizeof *ofr); + ofr->type = OFPT_FEATURES_REQUEST; + ofr->version = OFP_VERSION; + ofr->length = htons(sizeof *ofr); + queue_tx(sw, rconn, b); + + /* Send OFPT_SET_CONFIG. */ + b = buffer_new(0); + osc = buffer_put_uninit(b, sizeof *osc); + memset(osc, 0, sizeof *osc); + osc->header.type = OFPT_SET_CONFIG; + osc->header.version = OFP_VERSION; + osc->header.length = htons(sizeof *osc); + osc->flags = htons(OFPC_SEND_FLOW_EXP); + osc->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN); + queue_tx(sw, rconn, b); + + sw->last_features_request = now; + } +} + +static void +queue_tx(struct lswitch *sw, struct rconn *rconn, struct buffer *b) +{ + int retval = rconn_send(rconn, b); + if (retval) { + if (retval == EAGAIN) { + /* FIXME: ratelimit. */ + VLOG_WARN("%s: tx queue overflow", rconn_get_name(rconn)); + } else if (retval == ENOTCONN) { + /* Ignore. */ + } else { + /* FIXME: ratelimit. */ + VLOG_WARN("%s: send: %s", rconn_get_name(rconn), strerror(retval)); + } + buffer_delete(b); + } +} + +static void +process_packet_in(struct lswitch *sw, struct rconn *rconn, + struct ofp_packet_in *opi) +{ + uint16_t in_port = ntohs(opi->in_port); + uint16_t out_port = OFPP_FLOOD; + + size_t pkt_ofs, pkt_len; + struct buffer pkt; + struct flow flow; + + /* Extract flow data from 'opi' into 'flow'. */ + pkt_ofs = offsetof(struct ofp_packet_in, data); + pkt_len = ntohs(opi->header.length) - pkt_ofs; + pkt.data = opi->data; + pkt.size = pkt_len; + flow_extract(&pkt, in_port, &flow); + + if (sw->ml) { + if (mac_learning_learn(sw->ml, flow.dl_src, in_port)) { + VLOG_DBG("learned that "ETH_ADDR_FMT" is on datapath %" + PRIx64" port %"PRIu16, ETH_ADDR_ARGS(flow.dl_src), + ntohll(sw->datapath_id), in_port); + } + out_port = mac_learning_lookup(sw->ml, flow.dl_dst); + } + + if (sw->setup_flows && (!sw->ml || out_port != OFPP_FLOOD)) { + /* The output port is known, or we always flood everything, so add a + * new flow. */ + queue_tx(sw, rconn, make_add_simple_flow(&flow, ntohl(opi->buffer_id), + out_port)); + + /* If the switch didn't buffer the packet, we need to send a copy. */ + if (ntohl(opi->buffer_id) == UINT32_MAX) { + queue_tx(sw, rconn, + make_unbuffered_packet_out(&pkt, in_port, out_port)); + } + } else { + /* We don't know that MAC, or we don't set up flows. Send along the + * packet without setting up a flow. */ + struct buffer *b; + if (ntohl(opi->buffer_id) == UINT32_MAX) { + b = make_unbuffered_packet_out(&pkt, in_port, out_port); + } else { + b = make_buffered_packet_out(ntohl(opi->buffer_id), + in_port, out_port); + } + queue_tx(sw, rconn, b); + } +} diff --git a/lib/rconn.c b/lib/rconn.c index 4bfd5a42..5a5f4b19 100644 --- a/lib/rconn.c +++ b/lib/rconn.c @@ -57,6 +57,8 @@ struct rconn { int txq_limit; time_t backoff_deadline; int backoff; + time_t last_connected; + unsigned int packets_sent; }; static struct rconn *create_rconn(const char *name, int txq_limit, @@ -168,6 +170,7 @@ rconn_recv(struct rconn *rc) struct buffer *buffer; int error = vconn_recv(rc->vconn, &buffer); if (!error) { + rc->last_connected = time(0); return buffer; } else if (error != EAGAIN) { disconnect(rc, error); @@ -207,7 +210,7 @@ do_send(struct rconn *rc, struct buffer *b, int txq_limit) } /* Sends 'b' on 'rc'. Returns 0 if successful, EAGAIN if the send queue is - * full, otherwise a positive errno value. + * full, or ENOTCONN if 'rc' is not currently connected. * * There is no rconn_send_wait() function: an rconn has a send queue that it * takes care of sending if you call rconn_wait(), which will have the side @@ -237,6 +240,15 @@ rconn_is_full(const struct rconn *rc) return rc->txq.n >= rc->txq_limit; } +/* Returns the total number of packets successfully sent on the underlying + * vconn. A packet is not counted as sent while it is still queued in the + * rconn, only when it has been successfuly passed to the vconn. */ +unsigned int +rconn_packets_sent(const struct rconn *rc) +{ + return rc->packets_sent; +} + /* Returns 'rc''s name (the 'name' argument passed to rconn_new()). */ const char * rconn_get_name(const struct rconn *rc) @@ -258,6 +270,14 @@ rconn_is_connected(const struct rconn *rconn) { return rconn->vconn && !vconn_connect(rconn->vconn); } + +/* Returns 0 if 'rconn' is connected, otherwise the number of seconds that it + * has been disconnected. */ +int +rconn_disconnected_duration(const struct rconn *rconn) +{ + return rconn_is_connected(rconn) ? 0 : time(0) - rconn->last_connected; +} static struct rconn * create_rconn(const char *name, int txq_limit, struct vconn *vconn) @@ -272,6 +292,8 @@ create_rconn(const char *name, int txq_limit, struct vconn *vconn) rc->txq_limit = txq_limit; rc->backoff_deadline = 0; rc->backoff = 0; + rc->last_connected = time(0); + rc->packets_sent = 0; return rc; } @@ -286,6 +308,7 @@ try_send(struct rconn *rc) if (retval) { return retval; } + rc->packets_sent++; queue_advance_head(&rc->txq, next); return 0; }