2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <arpa/inet.h>
24 #include "dynamic-string.h"
28 #include "openflow/nicira-ext.h"
34 #define THIS_MODULE VLM_status
37 struct status_category {
40 void (*cb)(struct status_reply *, void *aux);
44 struct switch_status {
46 struct status_category *config_cat;
47 struct status_category *switch_cat;
48 struct list categories;
52 struct status_category *category;
58 switch_status_handle_request(struct switch_status *ss, struct rconn *rconn,
59 struct nicira_header *request)
61 struct status_category *c;
62 struct nicira_header *reply;
63 struct status_reply sr;
67 sr.request.string = (void *) (request + 1);
68 sr.request.length = ntohs(request->header.length) - sizeof *request;
70 LIST_FOR_EACH (c, struct status_category, node, &ss->categories) {
71 if (!memcmp(c->name, sr.request.string,
72 MIN(strlen(c->name), sr.request.length))) {
77 reply = make_openflow_xid(sizeof *reply + sr.output.length,
78 OFPT_VENDOR, request->header.xid, &b);
79 reply->vendor = htonl(NX_VENDOR_ID);
80 reply->subtype = htonl(NXT_STATUS_REPLY);
81 memcpy(reply + 1, sr.output.string, sr.output.length);
82 retval = rconn_send(rconn, b, NULL);
83 if (retval && retval != EAGAIN) {
84 VLOG_WARN("send failed (%s)", strerror(retval));
86 ds_destroy(&sr.output);
91 rconn_status_cb(struct status_reply *sr, void *rconn_)
93 struct rconn *rconn = rconn_;
94 time_t now = time_now();
96 status_reply_put(sr, "name=%s", rconn_get_name(rconn));
97 status_reply_put(sr, "state=%s", rconn_get_state(rconn));
98 status_reply_put(sr, "backoff=%d", rconn_get_backoff(rconn));
99 status_reply_put(sr, "is-connected=%s",
100 rconn_is_connected(rconn) ? "true" : "false");
101 status_reply_put(sr, "sent-msgs=%u", rconn_packets_sent(rconn));
102 status_reply_put(sr, "received-msgs=%u", rconn_packets_received(rconn));
103 status_reply_put(sr, "attempted-connections=%u",
104 rconn_get_attempted_connections(rconn));
105 status_reply_put(sr, "successful-connections=%u",
106 rconn_get_successful_connections(rconn));
107 status_reply_put(sr, "last-connection=%ld",
108 (long int) (now - rconn_get_last_connection(rconn)));
109 status_reply_put(sr, "time-connected=%lu",
110 rconn_get_total_time_connected(rconn));
111 status_reply_put(sr, "state-elapsed=%u", rconn_get_state_elapsed(rconn));
115 config_status_cb(struct status_reply *sr, void *ofproto_)
117 const struct ofproto *ofproto = ofproto_;
118 struct svec listeners;
119 int probe_interval, max_backoff;
122 svec_init(&listeners);
123 ofproto_get_listeners(ofproto, &listeners);
124 for (i = 0; i < listeners.n; i++) {
125 status_reply_put(sr, "management%zu=%s", i, listeners.names[i]);
127 svec_destroy(&listeners);
129 probe_interval = ofproto_get_probe_interval(ofproto);
130 if (probe_interval) {
131 status_reply_put(sr, "probe-interval=%d", probe_interval);
134 max_backoff = ofproto_get_max_backoff(ofproto);
136 status_reply_put(sr, "max-backoff=%d", max_backoff);
141 switch_status_cb(struct status_reply *sr, void *ss_)
143 struct switch_status *ss = ss_;
144 time_t now = time_now();
146 status_reply_put(sr, "now=%ld", (long int) now);
147 status_reply_put(sr, "uptime=%ld", (long int) (now - ss->booted));
148 status_reply_put(sr, "pid=%ld", (long int) getpid());
151 struct switch_status *
152 switch_status_create(const struct ofproto *ofproto)
154 struct switch_status *ss = xcalloc(1, sizeof *ss);
155 ss->booted = time_now();
156 list_init(&ss->categories);
157 ss->config_cat = switch_status_register(ss, "config", config_status_cb,
159 ss->switch_cat = switch_status_register(ss, "switch", switch_status_cb,
165 switch_status_destroy(struct switch_status *ss)
168 /* Orphan any remaining categories, so that unregistering them later
169 * won't write to bad memory. */
170 struct status_category *c, *next;
171 LIST_FOR_EACH_SAFE (c, next,
172 struct status_category, node, &ss->categories) {
175 switch_status_unregister(ss->config_cat);
176 switch_status_unregister(ss->switch_cat);
181 struct status_category *
182 switch_status_register(struct switch_status *ss,
183 const char *category,
184 status_cb_func *cb, void *aux)
186 struct status_category *c = xmalloc(sizeof *c);
189 c->name = xstrdup(category);
190 list_push_back(&ss->categories, &c->node);
195 switch_status_unregister(struct status_category *c)
198 if (!list_is_empty(&c->node)) {
199 list_remove(&c->node);
207 status_reply_put(struct status_reply *sr, const char *content, ...)
209 size_t old_length = sr->output.length;
213 /* Append the status reply to the output. */
214 ds_put_format(&sr->output, "%s.", sr->category->name);
215 va_start(args, content);
216 ds_put_format_valist(&sr->output, content, args);
218 if (ds_last(&sr->output) != '\n') {
219 ds_put_char(&sr->output, '\n');
222 /* Drop what we just added if it doesn't match the request. */
223 added = sr->output.length - old_length;
224 if (added < sr->request.length
225 || memcmp(&sr->output.string[old_length],
226 sr->request.string, sr->request.length)) {
227 ds_truncate(&sr->output, old_length);