27dc86cc87efcaa7006d8a87da0eb839cb4c6898
[sliver-openvswitch.git] / ofproto / status.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "status.h"
19 #include <arpa/inet.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include "dynamic-string.h"
26 #include "list.h"
27 #include "ofp-util.h"
28 #include "ofpbuf.h"
29 #include "ofproto.h"
30 #include "openflow/nicira-ext.h"
31 #include "packets.h"
32 #include "rconn.h"
33 #include "svec.h"
34 #include "timeval.h"
35 #include "vconn.h"
36
37 #define THIS_MODULE VLM_status
38 #include "vlog.h"
39
40 struct status_category {
41     struct list node;
42     char *name;
43     void (*cb)(struct status_reply *, void *aux);
44     void *aux;
45 };
46
47 struct switch_status {
48     time_t booted;
49     struct status_category *config_cat;
50     struct status_category *switch_cat;
51     struct list categories;
52 };
53
54 struct status_reply {
55     struct status_category *category;
56     struct ds request;
57     struct ds output;
58 };
59
60 int
61 switch_status_handle_request(struct switch_status *ss, struct rconn *rconn,
62                              struct nicira_header *request)
63 {
64     struct status_category *c;
65     struct nicira_header *reply;
66     struct status_reply sr;
67     struct ofpbuf *b;
68     int retval;
69
70     sr.request.string = (void *) (request + 1);
71     sr.request.length = ntohs(request->header.length) - sizeof *request;
72     ds_init(&sr.output);
73     LIST_FOR_EACH (c, struct status_category, node, &ss->categories) {
74         if (!memcmp(c->name, sr.request.string,
75                     MIN(strlen(c->name), sr.request.length))) {
76             sr.category = c;
77             c->cb(&sr, c->aux);
78         }
79     }
80     reply = make_openflow_xid(sizeof *reply + sr.output.length,
81                               OFPT_VENDOR, request->header.xid, &b);
82     reply->vendor = htonl(NX_VENDOR_ID);
83     reply->subtype = htonl(NXT_STATUS_REPLY);
84     memcpy(reply + 1, sr.output.string, sr.output.length);
85     retval = rconn_send(rconn, b, NULL);
86     if (retval && retval != EAGAIN) {
87         VLOG_WARN("send failed (%s)", strerror(retval));
88     }
89     ds_destroy(&sr.output);
90     return 0;
91 }
92
93 void
94 rconn_status_cb(struct status_reply *sr, void *rconn_)
95 {
96     struct rconn *rconn = rconn_;
97     time_t now = time_now();
98     uint32_t remote_ip = rconn_get_remote_ip(rconn);
99     uint32_t local_ip = rconn_get_local_ip(rconn);
100
101     status_reply_put(sr, "name=%s", rconn_get_target(rconn));
102     if (remote_ip) {
103         status_reply_put(sr, "remote-ip="IP_FMT, IP_ARGS(&remote_ip));
104         status_reply_put(sr, "remote-port=%d", 
105                          ntohs(rconn_get_remote_port(rconn)));
106         status_reply_put(sr, "local-ip="IP_FMT, IP_ARGS(&local_ip));
107         status_reply_put(sr, "local-port=%d", 
108                          ntohs(rconn_get_local_port(rconn)));
109     }
110     status_reply_put(sr, "state=%s", rconn_get_state(rconn));
111     status_reply_put(sr, "backoff=%d", rconn_get_backoff(rconn));
112     status_reply_put(sr, "probe-interval=%d", rconn_get_probe_interval(rconn));
113     status_reply_put(sr, "is-connected=%s",
114                      rconn_is_connected(rconn) ? "true" : "false");
115     status_reply_put(sr, "sent-msgs=%u", rconn_packets_sent(rconn));
116     status_reply_put(sr, "received-msgs=%u", rconn_packets_received(rconn));
117     status_reply_put(sr, "attempted-connections=%u",
118                      rconn_get_attempted_connections(rconn));
119     status_reply_put(sr, "successful-connections=%u",
120                      rconn_get_successful_connections(rconn));
121     status_reply_put(sr, "last-connection=%ld",
122                      (long int) (now - rconn_get_last_connection(rconn)));
123     status_reply_put(sr, "last-received=%ld",
124                      (long int) (now - rconn_get_last_received(rconn)));
125     status_reply_put(sr, "time-connected=%lu",
126                      rconn_get_total_time_connected(rconn));
127     status_reply_put(sr, "state-elapsed=%u", rconn_get_state_elapsed(rconn));
128 }
129
130 static void
131 config_status_cb(struct status_reply *sr, void *ofproto_)
132 {
133     const struct ofproto *ofproto = ofproto_;
134     uint64_t datapath_id;
135     struct svec listeners;
136     size_t i;
137
138     datapath_id = ofproto_get_datapath_id(ofproto);
139     if (datapath_id) {
140         status_reply_put(sr, "datapath-id=%016"PRIx64, datapath_id);
141     }
142
143     svec_init(&listeners);
144     ofproto_get_listeners(ofproto, &listeners);
145     for (i = 0; i < listeners.n; i++) {
146         status_reply_put(sr, "management%zu=%s", i, listeners.names[i]);
147     }
148     svec_destroy(&listeners);
149 }
150
151 static void
152 switch_status_cb(struct status_reply *sr, void *ss_)
153 {
154     struct switch_status *ss = ss_;
155     time_t now = time_now();
156
157     status_reply_put(sr, "now=%ld", (long int) now);
158     status_reply_put(sr, "uptime=%ld", (long int) (now - ss->booted));
159     status_reply_put(sr, "pid=%ld", (long int) getpid());
160 }
161
162 struct switch_status *
163 switch_status_create(const struct ofproto *ofproto)
164 {
165     struct switch_status *ss = xzalloc(sizeof *ss);
166     ss->booted = time_now();
167     list_init(&ss->categories);
168     ss->config_cat = switch_status_register(ss, "config", config_status_cb,
169                                             (void *) ofproto);
170     ss->switch_cat = switch_status_register(ss, "switch", switch_status_cb,
171                                             ss);
172     return ss;
173 }
174
175 void
176 switch_status_destroy(struct switch_status *ss)
177 {
178     if (ss) {
179         /* Orphan any remaining categories, so that unregistering them later
180          * won't write to bad memory. */
181         struct status_category *c, *next;
182         LIST_FOR_EACH_SAFE (c, next,
183                             struct status_category, node, &ss->categories) {
184             list_init(&c->node);
185         }
186         switch_status_unregister(ss->config_cat);
187         switch_status_unregister(ss->switch_cat);
188         free(ss);
189     }
190 }
191
192 struct status_category *
193 switch_status_register(struct switch_status *ss,
194                        const char *category,
195                        status_cb_func *cb, void *aux)
196 {
197     struct status_category *c = xmalloc(sizeof *c);
198     c->cb = cb;
199     c->aux = aux;
200     c->name = xstrdup(category);
201     list_push_back(&ss->categories, &c->node);
202     return c;
203 }
204
205 void
206 switch_status_unregister(struct status_category *c)
207 {
208     if (c) {
209         if (!list_is_empty(&c->node)) {
210             list_remove(&c->node);
211         }
212         free(c->name);
213         free(c);
214     }
215 }
216
217 void
218 status_reply_put(struct status_reply *sr, const char *content, ...)
219 {
220     size_t old_length = sr->output.length;
221     size_t added;
222     va_list args;
223
224     /* Append the status reply to the output. */
225     ds_put_format(&sr->output, "%s.", sr->category->name);
226     va_start(args, content);
227     ds_put_format_valist(&sr->output, content, args);
228     va_end(args);
229     if (ds_last(&sr->output) != '\n') {
230         ds_put_char(&sr->output, '\n');
231     }
232
233     /* Drop what we just added if it doesn't match the request. */
234     added = sr->output.length - old_length;
235     if (added < sr->request.length
236         || memcmp(&sr->output.string[old_length],
237                   sr->request.string, sr->request.length)) {
238         ds_truncate(&sr->output, old_length);
239     }
240 }