+\f
+/* OFPST_SWITCH statistics. */
+
+struct switch_status_category {
+ char *name;
+ void (*cb)(struct status_reply *, void *aux);
+ void *aux;
+};
+
+struct switch_status {
+ const struct settings *s;
+ time_t booted;
+ struct switch_status_category categories[8];
+ int n_categories;
+};
+
+struct status_reply {
+ struct switch_status_category *category;
+ struct ds request;
+ struct ds output;
+};
+
+static bool
+switch_status_packet_cb(struct relay *r, int half, void *ss_)
+{
+ struct switch_status *ss = ss_;
+ struct rconn *rc = r->halves[HALF_REMOTE].rconn;
+ struct buffer *msg = r->halves[HALF_REMOTE].rxbuf;
+ struct switch_status_category *c;
+ struct ofp_stats_request *osr;
+ struct ofp_stats_reply *reply;
+ struct status_reply sr;
+ struct ofp_header *oh;
+ struct buffer *b;
+ int retval;
+
+ if (half == HALF_LOCAL) {
+ return false;
+ }
+
+ oh = msg->data;
+ if (oh->type != OFPT_STATS_REQUEST) {
+ return false;
+ }
+ if (msg->size < sizeof(struct ofp_stats_request)) {
+ VLOG_WARN("packet too short (%zu bytes) for stats_request", msg->size);
+ return false;
+ }
+
+ osr = msg->data;
+ if (osr->type != htons(OFPST_SWITCH)) {
+ return false;
+ }
+
+ sr.request.string = (void *) (osr + 1);
+ sr.request.length = msg->size - sizeof *osr;
+ ds_init(&sr.output);
+ for (c = ss->categories; c < &ss->categories[ss->n_categories]; c++) {
+ if (!memcmp(c->name, sr.request.string,
+ MIN(strlen(c->name), sr.request.length))) {
+ sr.category = c;
+ c->cb(&sr, c->aux);
+ }
+ }
+ reply = make_openflow_xid((offsetof(struct ofp_stats_reply, body)
+ + sr.output.length),
+ OFPT_STATS_REPLY, osr->header.xid, &b);
+ reply->type = htons(OFPST_SWITCH);
+ reply->flags = 0;
+ memcpy(reply->body, sr.output.string, sr.output.length);
+ retval = rconn_send(rc, b, NULL);
+ if (retval && retval != EAGAIN) {
+ VLOG_WARN("send failed (%s)", strerror(retval));
+ }
+ ds_destroy(&sr.output);
+ return true;
+}
+
+static void
+rconn_status_cb(struct status_reply *sr, void *rconn_)
+{
+ struct rconn *rconn = rconn_;
+ time_t now = time_now();
+
+ status_reply_put(sr, "name=%s", rconn_get_name(rconn));
+ status_reply_put(sr, "state=%s", rconn_get_state(rconn));
+ status_reply_put(sr, "is-connected=%s",
+ rconn_is_connected(rconn) ? "true" : "false");
+ status_reply_put(sr, "sent-msgs=%u", rconn_packets_sent(rconn));
+ status_reply_put(sr, "received-msgs=%u", rconn_packets_received(rconn));
+ status_reply_put(sr, "attempted-connections=%u",
+ rconn_get_attempted_connections(rconn));
+ status_reply_put(sr, "successful-connections=%u",
+ rconn_get_successful_connections(rconn));
+ status_reply_put(sr, "last-connection=%ld",
+ (long int) (now - rconn_get_last_connection(rconn)));
+ status_reply_put(sr, "time-connected=%lu",
+ rconn_get_total_time_connected(rconn));
+}
+
+static void
+config_status_cb(struct status_reply *sr, void *s_)
+{
+ const struct settings *s = s_;
+
+ if (s->listen_vconn_name) {
+ status_reply_put(sr, "management=%s", s->listen_vconn_name);
+ }
+ if (s->probe_interval) {
+ status_reply_put(sr, "probe-interval=%d", s->probe_interval);
+ }
+ if (s->max_backoff) {
+ status_reply_put(sr, "max-backoff=%d", s->max_backoff);
+ }
+}
+
+static void
+switch_status_cb(struct status_reply *sr, void *ss_)
+{
+ struct switch_status *ss = ss_;
+ time_t now = time_now();
+
+ status_reply_put(sr, "now=%ld", (long int) now);
+ status_reply_put(sr, "uptime=%ld", (long int) (now - ss->booted));
+ status_reply_put(sr, "pid=%ld", (long int) getpid());
+}
+
+static struct hook
+switch_status_hook_create(const struct settings *s, struct switch_status **ssp)
+{
+ struct switch_status *ss = xcalloc(1, sizeof *ss);
+ ss->s = s;
+ ss->booted = time_now();
+ switch_status_register_category(ss, "config",
+ config_status_cb, (void *) s);
+ switch_status_register_category(ss, "switch", switch_status_cb, ss);
+ *ssp = ss;
+ return make_hook(switch_status_packet_cb, NULL, NULL, ss);
+}
+
+static void
+switch_status_register_category(struct switch_status *ss,
+ const char *category,
+ void (*cb)(struct status_reply *,
+ void *aux),
+ void *aux)
+{
+ struct switch_status_category *c;
+ assert(ss->n_categories < ARRAY_SIZE(ss->categories));
+ c = &ss->categories[ss->n_categories++];
+ c->cb = cb;
+ c->aux = aux;
+ c->name = xstrdup(category);
+}
+
+static void
+status_reply_put(struct status_reply *sr, const char *content, ...)
+{
+ size_t old_length = sr->output.length;
+ size_t added;
+ va_list args;
+
+ /* Append the status reply to the output. */
+ ds_put_format(&sr->output, "%s.", sr->category->name);
+ va_start(args, content);
+ ds_put_format_valist(&sr->output, content, args);
+ va_end(args);
+ if (ds_last(&sr->output) != '\n') {
+ ds_put_char(&sr->output, '\n');
+ }
+
+ /* Drop what we just added if it doesn't match the request. */
+ added = sr->output.length - old_length;
+ if (added < sr->request.length
+ || memcmp(&sr->output.string[old_length],
+ sr->request.string, sr->request.length)) {
+ ds_truncate(&sr->output, old_length);
+ }
+}
+