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, true);
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 die_if_already_running();
138 signal(SIGPIPE, SIG_IGN);
140 fatal_signal_block();
141 for (i = 0; i < n_ifaces; i++) {
142 struct iface *iface = &ifaces[i];
143 dhclient_run(iface->dhcp);
144 if (dhclient_changed(iface->dhcp)) {
145 bool is_bound = dhclient_is_bound(iface->dhcp);
148 /* Configure network device. */
149 if (!exit_without_bind) {
150 dhclient_configure_netdev(iface->dhcp);
151 dhclient_update_resolv_conf(iface->dhcp);
155 static bool detached = false;
158 /* Disable timeout, since discovery was successful. */
161 /* Print discovered parameters. */
163 dhcp_msg_to_string(dhclient_get_config(iface->dhcp),
165 fputs(ds_cstr(&ds), stdout);
170 /* Exit if the user requested it. */
171 if (exit_without_bind) {
172 VLOG_DBG("exiting because of successful binding on %s "
173 "and --exit-without-bind specified",
177 if (exit_after_bind) {
178 VLOG_DBG("exiting because of successful binding on %s "
179 "and --exit-after-bind specified",
184 /* Detach into background, if we haven't already. */
191 /* We only want an address on a single one of our interfaces.
192 * So: if we have an address on this interface, stop looking
193 * for one on the others; if we don't have an address on this
194 * interface, start looking everywhere. */
195 for (j = 0; j < n_ifaces; j++) {
196 struct iface *if2 = &ifaces[j];
199 dhclient_release(if2->dhcp);
201 dhclient_init(if2->dhcp, 0);
207 for (i = 0; i < n_ifaces; i++) {
208 struct iface *iface = &ifaces[i];
209 dhclient_wait(iface->dhcp);
211 fatal_signal_unblock();
219 iface_init(struct iface *iface, const char *netdev_name)
223 iface->name = netdev_name;
226 if (exit_after_bind) {
227 /* Bring this interface up permanently, so that the bound address
228 * persists past program termination. */
229 struct netdev *netdev;
231 retval = netdev_open(iface->name, NETDEV_ETH_TYPE_NONE, &netdev);
233 error(retval, "Could not open %s device", iface->name);
236 retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
238 error(retval, "Could not bring %s device up", iface->name);
241 netdev_close(netdev);
244 retval = dhclient_create(iface->name, modify_dhcp_request,
245 validate_dhcp_offer, NULL, &iface->dhcp);
247 error(retval, "%s: failed to initialize DHCP client", iface->name);
255 release_ifaces(void *aux UNUSED)
259 for (i = 0; i < n_ifaces; i++) {
260 struct dhclient *dhcp = ifaces[i].dhcp;
261 dhclient_release(dhcp);
262 if (dhclient_changed(dhcp)) {
263 dhclient_configure_netdev(dhcp);
269 modify_dhcp_request(struct dhcp_msg *msg, void *aux)
271 dhcp_msg_put_string(msg, DHCP_CODE_VENDOR_CLASS, "OpenFlow");
275 validate_dhcp_offer(const struct dhcp_msg *msg, void *aux)
277 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
281 vconn_name = dhcp_msg_get_string(msg, DHCP_CODE_OFP_CONTROLLER_VCONN);
283 VLOG_WARN_RL(&rl, "rejecting DHCP offer missing controller vconn");
286 accept = !regexec(&accept_controller_regex, vconn_name, 0, NULL, 0);
292 parse_options(int argc, char *argv[])
295 OPT_ACCEPT_VCONN = UCHAR_MAX + 1,
296 OPT_EXIT_WITHOUT_BIND,
300 static struct option long_options[] = {
301 {"accept-vconn", required_argument, 0, OPT_ACCEPT_VCONN},
302 {"exit-without-bind", no_argument, 0, OPT_EXIT_WITHOUT_BIND},
303 {"exit-after-bind", no_argument, 0, OPT_EXIT_AFTER_BIND},
304 {"no-detach", no_argument, 0, OPT_NO_DETACH},
305 {"timeout", required_argument, 0, 't'},
306 {"pidfile", optional_argument, 0, 'P'},
307 {"force", no_argument, 0, 'f'},
308 {"verbose", optional_argument, 0, 'v'},
309 {"help", no_argument, 0, 'h'},
310 {"version", no_argument, 0, 'V'},
313 char *short_options = long_options_to_short_options(long_options);
314 bool detach_after_bind = true;
317 unsigned long int timeout;
320 c = getopt_long(argc, argv, short_options, long_options, NULL);
326 case OPT_ACCEPT_VCONN:
327 accept_controller_re = (optarg[0] == '^'
329 : xasprintf("^%s", optarg));
332 case OPT_EXIT_WITHOUT_BIND:
333 exit_without_bind = true;
336 case OPT_EXIT_AFTER_BIND:
337 exit_after_bind = true;
341 detach_after_bind = false;
349 ignore_existing_pidfile();
353 timeout = strtoul(optarg, NULL, 10);
355 fatal(0, "value %s on -t or --timeout is not at least 1",
360 signal(SIGALRM, SIG_DFL);
367 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
371 vlog_set_verbosity(optarg);
383 if ((exit_without_bind + exit_after_bind + !detach_after_bind) > 1) {
384 fatal(0, "--exit-without-bind, --exit-after-bind, and --no-detach "
385 "are mutually exclusive");
387 if (detach_after_bind) {
395 printf("%s: a tool for discovering OpenFlow controllers.\n"
396 "usage: %s [OPTIONS] NETDEV [NETDEV...]\n"
397 "where each NETDEV is a network device on which to perform\n"
398 "controller discovery.\n"
399 "\nOrdinarily, ofp-discover runs in the foreground until it\n"
400 "obtains an IP address and discovers an OpenFlow controller via\n"
401 "DHCP, then it prints information about the controller to stdout\n"
402 "and detaches to the background to maintain the IP address lease.\n"
403 "\nNetworking options:\n"
404 " --accept-vconn=REGEX accept matching discovered controllers\n"
405 " --exit-without-bind exit after discovery, without binding\n"
406 " --exit-after-bind exit after discovery, after binding\n"
407 " --no-detach do not detach after discovery\n"
409 " -t, --timeout=SECS give up discovery after SECS seconds\n"
410 " -P, --pidfile[=FILE] create pidfile (default: %s/%s.pid)\n"
411 " -f, --force with -P, start even if already running\n"
412 " -v, --verbose=MODULE[:FACILITY[:LEVEL]] set logging levels\n"
413 " -v, --verbose set maximum verbosity level\n"
414 " -h, --help display this help message\n"
415 " -V, --version display version information\n",
416 program_name, program_name, RUNDIR, program_name);