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.
41 #include "command-line.h"
43 #include "dhcp-client.h"
45 #include "dynamic-string.h"
46 #include "fatal-signal.h"
48 #include "poll-loop.h"
51 #include "vlog-socket.h"
54 #define THIS_MODULE VLM_ofp_discover
58 struct dhclient *dhcp;
61 /* The interfaces that we serve. */
62 static struct iface *ifaces;
65 /* --accept-vconn: Regular expression specifying the class of controller vconns
66 * that we will accept during autodiscovery. */
67 static const char *accept_controller_re = ".*";
68 static regex_t accept_controller_regex;
70 /* --exit-without-bind: Exit after discovering the controller, without binding
71 * the network device to an IP address? */
72 static bool exit_without_bind;
74 /* --exit-after-bind: Exit after discovering the controller, after binding the
75 * network device to an IP address? */
76 static bool exit_after_bind;
78 static bool iface_init(struct iface *, const char *netdev_name);
79 static void release_ifaces(void *aux UNUSED);
81 static void parse_options(int argc, char *argv[]);
82 static void usage(void) NO_RETURN;
84 static void modify_dhcp_request(struct dhcp_msg *, void *aux);
85 static bool validate_dhcp_offer(const struct dhcp_msg *, void *aux);
88 main(int argc, char *argv[])
93 set_program_name(argv[0]);
96 parse_options(argc, argv);
101 fatal(0, "need at least one non-option argument; "
102 "use --help for usage");
105 ifaces = xmalloc(argc * sizeof *ifaces);
107 for (i = 0; i < argc; i++) {
108 if (iface_init(&ifaces[n_ifaces], argv[i])) {
113 fatal(0, "failed to initialize any DHCP clients");
116 for (i = 0; i < n_ifaces; i++) {
117 struct iface *iface = &ifaces[i];
118 dhclient_init(iface->dhcp, 0);
120 fatal_signal_add_hook(release_ifaces, NULL);
122 retval = regcomp(&accept_controller_regex, accept_controller_re,
123 REG_NOSUB | REG_EXTENDED);
125 size_t length = regerror(retval, &accept_controller_regex, NULL, 0);
126 char *buffer = xmalloc(length);
127 regerror(retval, &accept_controller_regex, buffer, length);
128 fatal(0, "%s: %s", accept_controller_re, buffer);
131 retval = vlog_server_listen(NULL, NULL);
133 fatal(retval, "Could not listen for vlog connections");
136 signal(SIGPIPE, SIG_IGN);
138 fatal_signal_block();
139 for (i = 0; i < n_ifaces; i++) {
140 struct iface *iface = &ifaces[i];
141 dhclient_run(iface->dhcp);
142 if (dhclient_changed(iface->dhcp)) {
143 bool is_bound = dhclient_is_bound(iface->dhcp);
146 /* Configure network device. */
147 if (!exit_without_bind) {
148 dhclient_configure_netdev(iface->dhcp);
149 dhclient_update_resolv_conf(iface->dhcp);
153 static bool detached = false;
156 /* Disable timeout, since discovery was successful. */
159 /* Print discovered parameters. */
161 dhcp_msg_to_string(dhclient_get_config(iface->dhcp),
163 fputs(ds_cstr(&ds), stdout);
168 /* Exit if the user requested it. */
169 if (exit_without_bind) {
170 VLOG_DBG("exiting because of successful binding on %s "
171 "and --exit-without-bind specified",
175 if (exit_after_bind) {
176 VLOG_DBG("exiting because of successful binding on %s "
177 "and --exit-after-bind specified",
182 /* Detach into background, if we haven't already. */
189 /* We only want an address on a single one of our interfaces.
190 * So: if we have an address on this interface, stop looking
191 * for one on the others; if we don't have an address on this
192 * interface, start looking everywhere. */
193 for (j = 0; j < n_ifaces; j++) {
194 struct iface *if2 = &ifaces[j];
197 dhclient_release(if2->dhcp);
199 dhclient_init(if2->dhcp, 0);
205 for (i = 0; i < n_ifaces; i++) {
206 struct iface *iface = &ifaces[i];
207 dhclient_wait(iface->dhcp);
209 fatal_signal_unblock();
217 iface_init(struct iface *iface, const char *netdev_name)
221 iface->name = netdev_name;
224 if (exit_after_bind) {
225 /* Bring this interface up permanently, so that the bound address
226 * persists past program termination. */
227 struct netdev *netdev;
229 retval = netdev_open(iface->name, NETDEV_ETH_TYPE_NONE, &netdev);
231 error(retval, "Could not open %s device", iface->name);
234 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
236 error(retval, "Could not bring %s device up", iface->name);
239 netdev_close(netdev);
242 retval = dhclient_create(iface->name, modify_dhcp_request,
243 validate_dhcp_offer, NULL, &iface->dhcp);
245 error(retval, "%s: failed to initialize DHCP client", iface->name);
253 release_ifaces(void *aux UNUSED)
257 for (i = 0; i < n_ifaces; i++) {
258 struct dhclient *dhcp = ifaces[i].dhcp;
259 dhclient_release(dhcp);
260 if (dhclient_changed(dhcp)) {
261 dhclient_configure_netdev(dhcp);
267 modify_dhcp_request(struct dhcp_msg *msg, void *aux)
269 dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
273 validate_dhcp_offer(const struct dhcp_msg *msg, void *aux)
278 vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
280 VLOG_WARN("rejecting DHCP offer missing controller vconn");
283 accept = !regexec(&accept_controller_regex, vconn_name, 0, NULL, 0);
289 parse_options(int argc, char *argv[])
292 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
293 OPT_EXIT_WITHOUT_BIND,
297 static struct option long_options[] = {
298 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
299 {"exit-without-bind", no_argument, 0, OPT_EXIT_WITHOUT_BIND},
300 {"exit-after-bind", no_argument, 0, OPT_EXIT_AFTER_BIND},
301 {"no-detach", no_argument, 0, OPT_NO_DETACH},
302 {"timeout", required_argument, 0, 't'},
303 {"pidfile", optional_argument, 0, 'P'},
304 {"verbose", optional_argument, 0, 'v'},
305 {"help", no_argument, 0, 'h'},
306 {"version", no_argument, 0, 'V'},
309 char *short_options = long_options_to_short_options(long_options);
310 bool detach_after_bind = true;
313 unsigned long int timeout;
316 c = getopt_long(argc, argv, short_options, long_options, NULL);
322 case OPT_ACCEPT_VCONN:
323 accept_controller_re = (optarg[0] == '^'
325 : xasprintf("^%s", optarg));
328 case OPT_EXIT_WITHOUT_BIND:
329 exit_without_bind = true;
332 case OPT_EXIT_AFTER_BIND:
333 exit_after_bind = true;
337 detach_after_bind = false;
345 timeout = strtoul(optarg, NULL, 10);
347 fatal(0, "value %s on -t or --timeout is not at least 1",
352 signal(SIGALRM, SIG_DFL);
359 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
363 vlog_set_verbosity(optarg);
375 if ((exit_without_bind + exit_after_bind + !detach_after_bind) > 1) {
376 fatal(0, "--exit-without-bind, --exit-after-bind, and --no-detach "
377 "are mutually exclusive");
379 if (detach_after_bind) {
387 printf("%s: a tool for discovering OpenFlow controllers.\n"
388 "usage: %s [OPTIONS] NETDEV [NETDEV...]\n"
389 "where each NETDEV is a network device on which to perform\n"
390 "controller discovery.\n"
391 "\nOrdinarily, ofp-discover runs in the foreground until it\n"
392 "obtains an IP address and discovers an OpenFlow controller via\n"
393 "DHCP, then it prints information about the controller to stdout\n"
394 "and detaches to the background to maintain the IP address lease.\n"
395 "\nNetworking options:\n"
396 " --accept-vconn=REGEX accept matching discovered controllers\n"
397 " --exit-without-bind exit after discovery, without binding\n"
398 " --exit-after-bind exit after discovery, after binding\n"
399 " --no-detach do not detach after discovery\n"
401 " -t, --timeout=SECS give up discovery after SECS seconds\n"
402 " -P, --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
403 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
404 " -v, --verbose set maximum verbosity level\n"
405 " -h, --help display this help message\n"
406 " -V, --version display version information\n",
407 program_name, program_name, RUNDIR, program_name);