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