vswitch: Don't pass null pointer to stat().
[sliver-openvswitch.git] / secchan / status.c
1 /*
2  * Copyright (c) 2008, 2009 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 <stdlib.h>
23 #include <unistd.h>
24 #include "dynamic-string.h"
25 #include "list.h"
26 #include "ofpbuf.h"
27 #include "ofproto.h"
28 #include "openflow/nicira-ext.h"
29 #include "rconn.h"
30 #include "svec.h"
31 #include "timeval.h"
32 #include "vconn.h"
33
34 #define THIS_MODULE VLM_status
35 #include "vlog.h"
36
37 struct status_category {
38     struct list node;
39     char *name;
40     void (*cb)(struct status_reply *, void *aux);
41     void *aux;
42 };
43
44 struct switch_status {
45     time_t booted;
46     struct status_category *config_cat;
47     struct status_category *switch_cat;
48     struct list categories;
49 };
50
51 struct status_reply {
52     struct status_category *category;
53     struct ds request;
54     struct ds output;
55 };
56
57 int
58 switch_status_handle_request(struct switch_status *ss, struct rconn *rconn,
59                              struct nicira_header *request)
60 {
61     struct status_category *c;
62     struct nicira_header *reply;
63     struct status_reply sr;
64     struct ofpbuf *b;
65     int retval;
66
67     sr.request.string = (void *) (request + 1);
68     sr.request.length = ntohs(request->header.length) - sizeof *request;
69     ds_init(&sr.output);
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))) {
73             sr.category = c;
74             c->cb(&sr, c->aux);
75         }
76     }
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));
85     }
86     ds_destroy(&sr.output);
87     return 0;
88 }
89
90 void
91 rconn_status_cb(struct status_reply *sr, void *rconn_)
92 {
93     struct rconn *rconn = rconn_;
94     time_t now = time_now();
95
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, "probe-interval=%d", rconn_get_probe_interval(rconn));
100     status_reply_put(sr, "is-connected=%s",
101                      rconn_is_connected(rconn) ? "true" : "false");
102     status_reply_put(sr, "sent-msgs=%u", rconn_packets_sent(rconn));
103     status_reply_put(sr, "received-msgs=%u", rconn_packets_received(rconn));
104     status_reply_put(sr, "attempted-connections=%u",
105                      rconn_get_attempted_connections(rconn));
106     status_reply_put(sr, "successful-connections=%u",
107                      rconn_get_successful_connections(rconn));
108     status_reply_put(sr, "last-connection=%ld",
109                      (long int) (now - rconn_get_last_connection(rconn)));
110     status_reply_put(sr, "last-received=%ld",
111                      (long int) (now - rconn_get_last_received(rconn)));
112     status_reply_put(sr, "time-connected=%lu",
113                      rconn_get_total_time_connected(rconn));
114     status_reply_put(sr, "state-elapsed=%u", rconn_get_state_elapsed(rconn));
115 }
116
117 static void
118 config_status_cb(struct status_reply *sr, void *ofproto_)
119 {
120     const struct ofproto *ofproto = ofproto_;
121     struct svec listeners;
122     int probe_interval, max_backoff;
123     size_t i;
124
125     svec_init(&listeners);
126     ofproto_get_listeners(ofproto, &listeners);
127     for (i = 0; i < listeners.n; i++) {
128         status_reply_put(sr, "management%zu=%s", i, listeners.names[i]);
129     }
130     svec_destroy(&listeners);
131
132     probe_interval = ofproto_get_probe_interval(ofproto);
133     if (probe_interval) {
134         status_reply_put(sr, "probe-interval=%d", probe_interval);
135     }
136
137     max_backoff = ofproto_get_max_backoff(ofproto);
138     if (max_backoff) {
139         status_reply_put(sr, "max-backoff=%d", max_backoff);
140     }
141 }
142
143 static void
144 switch_status_cb(struct status_reply *sr, void *ss_)
145 {
146     struct switch_status *ss = ss_;
147     time_t now = time_now();
148
149     status_reply_put(sr, "now=%ld", (long int) now);
150     status_reply_put(sr, "uptime=%ld", (long int) (now - ss->booted));
151     status_reply_put(sr, "pid=%ld", (long int) getpid());
152 }
153
154 struct switch_status *
155 switch_status_create(const struct ofproto *ofproto)
156 {
157     struct switch_status *ss = xcalloc(1, sizeof *ss);
158     ss->booted = time_now();
159     list_init(&ss->categories);
160     ss->config_cat = switch_status_register(ss, "config", config_status_cb,
161                                             (void *) ofproto);
162     ss->switch_cat = switch_status_register(ss, "switch", switch_status_cb,
163                                             ss);
164     return ss;
165 }
166
167 void
168 switch_status_destroy(struct switch_status *ss)
169 {
170     if (ss) {
171         /* Orphan any remaining categories, so that unregistering them later
172          * won't write to bad memory. */
173         struct status_category *c, *next;
174         LIST_FOR_EACH_SAFE (c, next,
175                             struct status_category, node, &ss->categories) {
176             list_init(&c->node);
177         }
178         switch_status_unregister(ss->config_cat);
179         switch_status_unregister(ss->switch_cat);
180         free(ss);
181     }
182 }
183
184 struct status_category *
185 switch_status_register(struct switch_status *ss,
186                        const char *category,
187                        status_cb_func *cb, void *aux)
188 {
189     struct status_category *c = xmalloc(sizeof *c);
190     c->cb = cb;
191     c->aux = aux;
192     c->name = xstrdup(category);
193     list_push_back(&ss->categories, &c->node);
194     return c;
195 }
196
197 void
198 switch_status_unregister(struct status_category *c)
199 {
200     if (c) {
201         if (!list_is_empty(&c->node)) {
202             list_remove(&c->node);
203         }
204         free(c->name);
205         free(c);
206     }
207 }
208
209 void
210 status_reply_put(struct status_reply *sr, const char *content, ...)
211 {
212     size_t old_length = sr->output.length;
213     size_t added;
214     va_list args;
215
216     /* Append the status reply to the output. */
217     ds_put_format(&sr->output, "%s.", sr->category->name);
218     va_start(args, content);
219     ds_put_format_valist(&sr->output, content, args);
220     va_end(args);
221     if (ds_last(&sr->output) != '\n') {
222         ds_put_char(&sr->output, '\n');
223     }
224
225     /* Drop what we just added if it doesn't match the request. */
226     added = sr->output.length - old_length;
227     if (added < sr->request.length
228         || memcmp(&sr->output.string[old_length],
229                   sr->request.string, sr->request.length)) {
230         ds_truncate(&sr->output, old_length);
231     }
232 }