Merge "master" branch into "wdp".
[sliver-openvswitch.git] / ofproto / discovery.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 "discovery.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/socket.h>
22 #include <net/if.h>
23 #include <regex.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "dhcp-client.h"
27 #include "dhcp.h"
28 #include "netdev.h"
29 #include "openflow/openflow.h"
30 #include "packets.h"
31 #include "status.h"
32 #include "stream-ssl.h"
33 #include "wdp.h"
34 #include "xfif.h"
35
36 #define THIS_MODULE VLM_discovery
37 #include "vlog.h"
38
39 struct discovery {
40     char *re;
41     bool update_resolv_conf;
42     regex_t *regex;
43     struct dhclient *dhcp;
44     int n_changes;
45     struct status_category *ss_cat;
46 };
47
48 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
49 static bool validate_dhcp_offer(const struct dhcp_msg *, void *aux);
50
51 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
52
53 static void
54 discovery_status_cb(struct status_reply *sr, void *d_)
55 {
56     struct discovery *d = d_;
57
58     status_reply_put(sr, "accept-remote=%s", d->re);
59     status_reply_put(sr, "n-changes=%d", d->n_changes);
60     if (d->dhcp) {
61         status_reply_put(sr, "state=%s", dhclient_get_state(d->dhcp));
62         status_reply_put(sr, "state-elapsed=%u",
63                          dhclient_get_state_elapsed(d->dhcp)); 
64         if (dhclient_is_bound(d->dhcp)) {
65             uint32_t ip = dhclient_get_ip(d->dhcp);
66             uint32_t netmask = dhclient_get_netmask(d->dhcp);
67             uint32_t router = dhclient_get_router(d->dhcp);
68
69             const struct dhcp_msg *cfg = dhclient_get_config(d->dhcp);
70             uint32_t dns_server;
71             char *domain_name;
72             int i;
73
74             status_reply_put(sr, "ip="IP_FMT, IP_ARGS(&ip));
75             status_reply_put(sr, "netmask="IP_FMT, IP_ARGS(&netmask));
76             if (router) {
77                 status_reply_put(sr, "router="IP_FMT, IP_ARGS(&router));
78             }
79
80             for (i = 0; dhcp_msg_get_ip(cfg, DHCP_CODE_DNS_SERVER, i,
81                                         &dns_server);
82                  i++) {
83                 status_reply_put(sr, "dns%d="IP_FMT, i, IP_ARGS(&dns_server));
84             }
85
86             domain_name = dhcp_msg_get_string(cfg, DHCP_CODE_DOMAIN_NAME);
87             if (domain_name) {
88                 status_reply_put(sr, "domain=%s", domain_name);
89                 free(domain_name);
90             }
91
92             status_reply_put(sr, "lease-remaining=%u",
93                              dhclient_get_lease_remaining(d->dhcp));
94         }
95     }
96 }
97
98 int
99 discovery_create(const char *re, bool update_resolv_conf,
100                  struct wdp *wdp, struct switch_status *ss,
101                  struct discovery **discoveryp)
102 {
103     struct discovery *d;
104     char *local_name;
105     int error;
106
107     d = xzalloc(sizeof *d);
108
109     /* Controller regular expression. */
110     error = discovery_set_accept_controller_re(d, re);
111     if (error) {
112         goto error_free;
113     }
114     d->update_resolv_conf = update_resolv_conf;
115
116     /* Initialize DHCP client. */
117     error = wdp_port_get_name(wdp, OFPP_LOCAL, &local_name);
118     if (error) {
119         VLOG_ERR("failed to query datapath local port: %s", strerror(error));
120         goto error_regfree;
121     }
122     error = dhclient_create(local_name, modify_dhcp_request,
123                             validate_dhcp_offer, d, &d->dhcp);
124     free(local_name);
125     if (error) {
126         VLOG_ERR("failed to initialize DHCP client: %s", strerror(error));
127         goto error_regfree;
128     }
129     dhclient_set_max_timeout(d->dhcp, 3);
130     dhclient_init(d->dhcp, 0);
131
132     d->ss_cat = switch_status_register(ss, "discovery",
133                                        discovery_status_cb, d);
134
135     *discoveryp = d;
136     return 0;
137
138 error_regfree:
139     regfree(d->regex);
140     free(d->regex);
141 error_free:
142     free(d);
143     *discoveryp = 0;
144     return error;
145 }
146
147 void
148 discovery_destroy(struct discovery *d)
149 {
150     if (d) {
151         free(d->re);
152         regfree(d->regex);
153         free(d->regex);
154         dhclient_destroy(d->dhcp);
155         switch_status_unregister(d->ss_cat);
156         free(d);
157     }
158 }
159
160 bool
161 discovery_get_update_resolv_conf(const struct discovery *d)
162 {
163     return d->update_resolv_conf;
164 }
165
166 void
167 discovery_set_update_resolv_conf(struct discovery *d,
168                                  bool update_resolv_conf)
169 {
170     d->update_resolv_conf = update_resolv_conf;
171 }
172
173 const char *
174 discovery_get_accept_controller_re(const struct discovery *d)
175 {
176     return d->re;
177 }
178
179 int
180 discovery_set_accept_controller_re(struct discovery *d, const char *re_)
181 {
182     regex_t *regex;
183     int error;
184     char *re;
185
186     re = (!re_ ? xstrdup(stream_ssl_is_configured() ? "^ssl:.*" : "^tcp:.*")
187           : re_[0] == '^' ? xstrdup(re_) : xasprintf("^%s", re_));
188     regex = xmalloc(sizeof *regex);
189     error = regcomp(regex, re, REG_NOSUB | REG_EXTENDED);
190     if (error) {
191         size_t length = regerror(error, regex, NULL, 0);
192         char *buffer = xmalloc(length);
193         regerror(error, regex, buffer, length);
194         VLOG_WARN("%s: %s", re, buffer);
195         free(regex);
196         free(re);
197         return EINVAL;
198     } else {
199         if (d->regex) {
200             regfree(d->regex);
201             free(d->regex);
202         }
203         free(d->re);
204
205         d->regex = regex;
206         d->re = re;
207         return 0;
208     }
209 }
210
211 void
212 discovery_question_connectivity(struct discovery *d)
213 {
214     if (d->dhcp) {
215         dhclient_force_renew(d->dhcp, 15); 
216     }
217 }
218
219 bool
220 discovery_run(struct discovery *d, char **controller_name)
221 {
222     if (!d->dhcp) {
223         *controller_name = NULL;
224         return true;
225     }
226
227     dhclient_run(d->dhcp);
228     if (!dhclient_changed(d->dhcp)) {
229         return false;
230     }
231
232     dhclient_configure_netdev(d->dhcp);
233     if (d->update_resolv_conf) {
234         dhclient_update_resolv_conf(d->dhcp);
235     }
236
237     if (dhclient_is_bound(d->dhcp)) {
238         *controller_name = dhcp_msg_get_string(dhclient_get_config(d->dhcp),
239                                                DHCP_CODE_OFP_CONTROLLER_VCONN);
240         VLOG_INFO("%s: discovered controller", *controller_name);
241         d->n_changes++;
242     } else {
243         *controller_name = NULL;
244         if (d->n_changes) {
245             VLOG_INFO("discovered controller no longer available");
246             d->n_changes++;
247         }
248     }
249     return true;
250 }
251
252 void
253 discovery_wait(struct discovery *d)
254 {
255     if (d->dhcp) {
256         dhclient_wait(d->dhcp); 
257     }
258 }
259
260 static void
261 modify_dhcp_request(struct dhcp_msg *msg, void *aux OVS_UNUSED)
262 {
263     dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
264 }
265
266 static bool
267 validate_dhcp_offer(const struct dhcp_msg *msg, void *d_)
268 {
269     const struct discovery *d = d_;
270     char *vconn_name;
271     bool accept;
272
273     vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
274     if (!vconn_name) {
275         VLOG_WARN_RL(&rl, "rejecting DHCP offer missing controller vconn");
276         return false;
277     }
278     accept = !regexec(d->regex, vconn_name, 0, NULL, 0);
279     if (!accept) {
280         VLOG_WARN_RL(&rl, "rejecting controller vconn that fails to match %s",
281                      d->re);
282     }
283     free(vconn_name);
284     return accept;
285 }