1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
35 #include "discovery.h"
39 #include "dhcp-client.h"
44 #include "port-watcher.h"
48 #define THIS_MODULE VLM_discovery
53 const struct settings *s;
54 struct dhclient *dhcp;
58 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
59 static bool validate_dhcp_offer(const struct dhcp_msg *, void *aux);
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
64 discovery_status_cb(struct status_reply *sr, void *d_)
66 struct discovery *d = d_;
68 status_reply_put(sr, "accept-remote=%s", d->s->accept_controller_re);
69 status_reply_put(sr, "n-changes=%d", d->n_changes);
71 status_reply_put(sr, "state=%s", dhclient_get_state(d->dhcp));
72 status_reply_put(sr, "state-elapsed=%u",
73 dhclient_get_state_elapsed(d->dhcp));
74 if (dhclient_is_bound(d->dhcp)) {
75 uint32_t ip = dhclient_get_ip(d->dhcp);
76 uint32_t netmask = dhclient_get_netmask(d->dhcp);
77 uint32_t router = dhclient_get_router(d->dhcp);
79 const struct dhcp_msg *cfg = dhclient_get_config(d->dhcp);
84 status_reply_put(sr, "ip="IP_FMT, IP_ARGS(&ip));
85 status_reply_put(sr, "netmask="IP_FMT, IP_ARGS(&netmask));
87 status_reply_put(sr, "router="IP_FMT, IP_ARGS(&router));
90 for (i = 0; dhcp_msg_get_ip(cfg, DHCP_CODE_DNS_SERVER, i,
93 status_reply_put(sr, "dns%d="IP_FMT, i, IP_ARGS(&dns_server));
96 domain_name = dhcp_msg_get_string(cfg, DHCP_CODE_DOMAIN_NAME);
98 status_reply_put(sr, "domain=%s", domain_name);
102 status_reply_put(sr, "lease-remaining=%u",
103 dhclient_get_lease_remaining(d->dhcp));
109 discovery_local_port_cb(const struct ofp_phy_port *port, void *d_)
111 struct discovery *d = d_;
113 char name[OFP_MAX_PORT_NAME_LEN + 1];
114 struct netdev *netdev;
117 /* Check that this was really a change. */
118 get_port_name(port, name, sizeof name);
119 if (d->dhcp && !strcmp(netdev_get_name(dhclient_get_netdev(d->dhcp)),
124 /* Destroy current DHCP client. */
125 dhclient_destroy(d->dhcp);
128 /* Bring local network device up. */
129 retval = netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev);
131 VLOG_ERR("Could not open %s device, discovery disabled: %s",
132 name, strerror(retval));
135 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
137 VLOG_ERR("Could not bring %s device up, discovery disabled: %s",
138 name, strerror(retval));
141 netdev_close(netdev);
143 /* Initialize DHCP client. */
144 retval = dhclient_create(name, modify_dhcp_request,
145 validate_dhcp_offer, (void *) d->s, &d->dhcp);
147 VLOG_ERR("Failed to initialize DHCP client, "
148 "discovery disabled: %s", strerror(retval));
151 dhclient_set_max_timeout(d->dhcp, 3);
152 dhclient_init(d->dhcp, 0);
154 dhclient_destroy(d->dhcp);
161 discovery_init(const struct settings *s, struct port_watcher *pw,
162 struct switch_status *ss)
166 d = xmalloc(sizeof *d);
171 switch_status_register_category(ss, "discovery", discovery_status_cb, d);
172 port_watcher_register_local_port_callback(pw, discovery_local_port_cb, d);
178 discovery_question_connectivity(struct discovery *d)
181 dhclient_force_renew(d->dhcp, 15);
186 discovery_run(struct discovery *d, char **controller_name)
189 *controller_name = NULL;
193 dhclient_run(d->dhcp);
194 if (!dhclient_changed(d->dhcp)) {
198 dhclient_configure_netdev(d->dhcp);
199 if (d->s->update_resolv_conf) {
200 dhclient_update_resolv_conf(d->dhcp);
203 if (dhclient_is_bound(d->dhcp)) {
204 *controller_name = dhcp_msg_get_string(dhclient_get_config(d->dhcp),
205 DHCP_CODE_OFP_CONTROLLER_VCONN);
206 VLOG_WARN("%s: discovered controller", *controller_name);
209 *controller_name = NULL;
211 VLOG_WARN("discovered controller no longer available");
219 discovery_wait(struct discovery *d)
222 dhclient_wait(d->dhcp);
227 modify_dhcp_request(struct dhcp_msg *msg, void *aux)
229 dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
233 validate_dhcp_offer(const struct dhcp_msg *msg, void *s_)
235 const struct settings *s = s_;
239 vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
241 VLOG_WARN_RL(&rl, "rejecting DHCP offer missing controller vconn");
244 accept = !regexec(&s->accept_controller_regex, vconn_name, 0, NULL, 0);
246 VLOG_WARN_RL(&rl, "rejecting controller vconn that fails to match %s",
247 s->accept_controller_re);