1 /* Copyright (c) 2008, 2009 Nicira Networks
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <linux/genetlink.h>
27 #include <linux/rtnetlink.h>
31 #include <sys/types.h>
37 #include "command-line.h"
42 #include "fatal-signal.h"
44 #include "leak-checker.h"
48 #include "openvswitch/brcompat-netlink.h"
49 #include "poll-loop.h"
58 #define THIS_MODULE VLM_brcompatd
61 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
63 /* Actions to modify bridge compatibility configuration. */
71 static void parse_options(int argc, char *argv[]);
72 static void usage(void) NO_RETURN;
74 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
76 /* Maximum number of milliseconds to wait for the config file to be
77 * unlocked. If set to zero, no waiting will occur. */
78 static int lock_timeout = 500;
80 /* Maximum number of milliseconds to wait before pruning port entries that
81 * no longer exist. If set to zero, ports are never pruned. */
82 static int prune_timeout = 5000;
84 /* Config file shared with ovs-vswitchd (usually ovs-vswitchd.conf). */
85 static char *config_file;
87 /* Command to run (via system()) to reload the ovs-vswitchd configuration
89 static char *reload_command;
91 /* Netlink socket to listen for interface changes. */
92 static struct nl_sock *rtnl_sock;
94 /* Netlink socket to bridge compatibility kernel module. */
95 static struct nl_sock *brc_sock;
97 /* The Generic Netlink family number used for bridge compatibility. */
98 static int brc_family;
100 static const struct nl_policy brc_multicast_policy[] = {
101 [BRC_GENL_A_MC_GROUP] = {.type = NL_A_U32 }
104 static const struct nl_policy rtnlgrp_link_policy[] = {
105 [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
106 [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
110 lookup_brc_multicast_group(int *multicast_group)
112 struct nl_sock *sock;
113 struct ofpbuf request, *reply;
114 struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
117 retval = nl_sock_create(NETLINK_GENERIC, 0, 0, 0, &sock);
121 ofpbuf_init(&request, 0);
122 nl_msg_put_genlmsghdr(&request, sock, 0, brc_family,
123 NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
124 retval = nl_sock_transact(sock, &request, &reply);
125 ofpbuf_uninit(&request);
127 nl_sock_destroy(sock);
130 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
131 brc_multicast_policy, attrs,
132 ARRAY_SIZE(brc_multicast_policy))) {
133 nl_sock_destroy(sock);
134 ofpbuf_delete(reply);
137 *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
138 nl_sock_destroy(sock);
139 ofpbuf_delete(reply);
144 /* Opens a socket for brcompat notifications. Returns 0 if successful,
145 * otherwise a positive errno value. */
147 brc_open(struct nl_sock **sock)
149 int multicast_group = 0;
152 retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
157 retval = lookup_brc_multicast_group(&multicast_group);
162 retval = nl_sock_create(NETLINK_GENERIC, multicast_group, 0, 0, sock);
170 static const struct nl_policy brc_dp_policy[] = {
171 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
175 bridge_exists(const char *name)
177 return cfg_has_section("bridge.%s", name);
181 rewrite_and_reload_config(void)
183 if (cfg_is_dirty()) {
184 int error1 = cfg_write();
185 int error2 = cfg_read();
186 long long int reload_start = time_msec();
187 int error3 = system(reload_command);
188 long long int elapsed = time_msec() - reload_start;
189 COVERAGE_INC(brcompatd_reload);
191 VLOG_INFO("reload command executed in %lld ms", elapsed);
194 VLOG_ERR("failed to execute reload command: %s", strerror(errno));
195 } else if (error3 != 0) {
196 char *msg = process_status_msg(error3);
197 VLOG_ERR("reload command exited with error (%s)", msg);
200 return error1 ? error1 : error2 ? error2 : error3 ? ECHILD : 0;
205 /* Go through the configuration file and remove any ports that no longer
206 * exist associated with a bridge. */
212 struct svec bridges, delete;
214 if (cfg_lock(NULL, 0)) {
215 /* Couldn't lock config file. */
221 cfg_get_subsections(&bridges, "bridge");
222 for (i=0; i<bridges.n; i++) {
223 const char *br_name = bridges.names[i];
224 struct svec ports, ifaces;
228 /* Get all the interfaces for the given bridge, breaking bonded
229 * interfaces down into their constituent parts. */
231 cfg_get_all_keys(&ports, "bridge.%s.port", br_name);
232 for (j=0; j<ports.n; j++) {
233 const char *port_name = ports.names[j];
234 if (cfg_has_section("bonding.%s", port_name)) {
237 cfg_get_all_keys(&slaves, "bonding.%s.slave", port_name);
238 svec_append(&ifaces, &slaves);
239 svec_destroy(&slaves);
241 svec_add(&ifaces, port_name);
244 svec_destroy(&ports);
246 /* Check that the interfaces exist. */
247 for (j = 0; j < ifaces.n; j++) {
248 const char *iface_name = ifaces.names[j];
249 enum netdev_flags flags;
251 /* The local port and internal ports are created and destroyed by
252 * ovs-vswitchd itself, so don't bother checking for them at all.
253 * In practice, they might not exist if ovs-vswitchd hasn't
254 * finished reloading since the configuration file was updated. */
255 if (!strcmp(iface_name, br_name)
256 || cfg_get_bool(0, "iface.%s.internal", iface_name)) {
260 error = netdev_nodev_get_flags(iface_name, &flags);
261 if (error == ENODEV) {
262 VLOG_DBG_RL(&rl, "removing dead interface %s from %s",
263 iface_name, br_name);
264 svec_add(&delete, iface_name);
266 VLOG_DBG_RL(&rl, "unknown error %d on interface %s from %s",
267 error, iface_name, br_name);
270 svec_destroy(&ifaces);
272 svec_destroy(&bridges);
277 for (i = 0; i < delete.n; i++) {
278 cfg_del_match("bridge.*.port=%s", delete.names[i]);
279 cfg_del_match("bonding.*.slave=%s", delete.names[i]);
281 rewrite_and_reload_config();
286 svec_destroy(&delete);
290 /* Checks whether a network device named 'name' exists and returns true if so,
293 * XXX it is possible that this doesn't entirely accomplish what we want in
294 * context, since ovs-vswitchd.conf may cause vswitchd to create or destroy
295 * network devices based on iface.*.internal settings.
297 * XXX may want to move this to lib/netdev. */
299 netdev_exists(const char *name)
305 filename = xasprintf("/sys/class/net/%s", name);
306 error = stat(filename, &s);
312 add_bridge(const char *br_name)
314 if (bridge_exists(br_name)) {
315 VLOG_WARN("addbr %s: bridge %s exists", br_name, br_name);
317 } else if (netdev_exists(br_name)) {
318 if (cfg_get_bool(0, "iface.%s.fake-bridge", br_name)) {
319 VLOG_WARN("addbr %s: %s exists as a fake bridge",
323 VLOG_WARN("addbr %s: cannot create bridge %s because a network "
324 "device named %s already exists",
325 br_name, br_name, br_name);
330 cfg_add_entry("bridge.%s.port=%s", br_name, br_name);
331 VLOG_INFO("addbr %s: success", br_name);
337 del_bridge(const char *br_name)
339 if (!bridge_exists(br_name)) {
340 VLOG_WARN("delbr %s: no bridge named %s", br_name, br_name);
344 cfg_del_section("bridge.%s", br_name);
345 VLOG_INFO("delbr %s: success", br_name);
351 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
352 const char **port_name)
354 static const struct nl_policy policy[] = {
355 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
356 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
358 struct nlattr *attrs[ARRAY_SIZE(policy)];
360 if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
361 attrs, ARRAY_SIZE(policy))
362 || (port_name && !attrs[BRC_GENL_A_PORT_NAME])) {
366 *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
367 *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
369 *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
375 send_reply(uint32_t seq, int error)
381 ofpbuf_init(&msg, 0);
382 nl_msg_put_genlmsghdr(&msg, brc_sock, 32, brc_family, NLM_F_REQUEST,
383 BRC_GENL_C_DP_RESULT, 1);
384 ((struct nlmsghdr *) msg.data)->nlmsg_seq = seq;
385 nl_msg_put_u32(&msg, BRC_GENL_A_ERR_CODE, error);
388 retval = nl_sock_send(brc_sock, &msg, false);
390 VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
397 handle_bridge_cmd(struct ofpbuf *buffer, bool add)
403 error = parse_command(buffer, &seq, &br_name, NULL);
405 error = add ? add_bridge(br_name) : del_bridge(br_name);
407 error = rewrite_and_reload_config();
409 send_reply(seq, error);
414 static const struct nl_policy brc_port_policy[] = {
415 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
416 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
420 del_port(const char *br_name, const char *port_name)
422 cfg_del_entry("bridge.%s.port=%s", br_name, port_name);
423 cfg_del_match("bonding.*.slave=%s", port_name);
424 cfg_del_match("vlan.%s.*", port_name);
428 handle_port_cmd(struct ofpbuf *buffer, bool add)
430 const char *cmd_name = add ? "add-if" : "del-if";
431 const char *br_name, *port_name;
435 error = parse_command(buffer, &seq, &br_name, &port_name);
437 if (!bridge_exists(br_name)) {
438 VLOG_WARN("%s %s %s: no bridge named %s",
439 cmd_name, br_name, port_name, br_name);
441 } else if (!netdev_exists(port_name)) {
442 VLOG_WARN("%s %s %s: no network device named %s",
443 cmd_name, br_name, port_name, port_name);
447 cfg_add_entry("bridge.%s.port=%s", br_name, port_name);
449 del_port(br_name, port_name);
451 VLOG_INFO("%s %s %s: success", cmd_name, br_name, port_name);
452 error = rewrite_and_reload_config();
454 send_reply(seq, error);
461 brc_recv_update(void)
464 struct ofpbuf *buffer;
465 struct genlmsghdr *genlmsghdr;
470 ofpbuf_delete(buffer);
471 retval = nl_sock_recv(brc_sock, &buffer, false);
472 } while (retval == ENOBUFS
474 && (nl_msg_nlmsgerr(buffer, NULL)
475 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE)));
477 if (retval != EAGAIN) {
478 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
483 genlmsghdr = nl_msg_genlmsghdr(buffer);
485 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
489 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
490 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
491 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
495 if (cfg_lock(NULL, lock_timeout)) {
496 /* Couldn't lock config file. */
501 switch (genlmsghdr->cmd) {
502 case BRC_GENL_C_DP_ADD:
503 retval = handle_bridge_cmd(buffer, true);
506 case BRC_GENL_C_DP_DEL:
507 retval = handle_bridge_cmd(buffer, false);
510 case BRC_GENL_C_PORT_ADD:
511 retval = handle_port_cmd(buffer, true);
514 case BRC_GENL_C_PORT_DEL:
515 retval = handle_port_cmd(buffer, false);
525 ofpbuf_delete(buffer);
529 /* Check for interface configuration changes announced through RTNL. */
531 rtnl_recv_update(void)
535 int error = nl_sock_recv(rtnl_sock, &buf, false);
536 if (error == EAGAIN) {
538 } else if (error == ENOBUFS) {
539 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
541 VLOG_WARN_RL(&rl, "error on network monitor socket: %s",
544 struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
545 struct nlmsghdr *nlh;
546 struct ifinfomsg *iim;
548 nlh = ofpbuf_at(buf, 0, NLMSG_HDRLEN);
549 iim = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *iim);
551 VLOG_WARN_RL(&rl, "received bad rtnl message (no ifinfomsg)");
556 if (!nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
558 attrs, ARRAY_SIZE(rtnlgrp_link_policy))) {
559 VLOG_WARN_RL(&rl,"received bad rtnl message (policy)");
563 if (nlh->nlmsg_type == RTM_DELLINK && attrs[IFLA_MASTER]) {
564 const char *port_name = nl_attr_get_string(attrs[IFLA_IFNAME]);
565 char br_name[IFNAMSIZ];
566 uint32_t br_idx = nl_attr_get_u32(attrs[IFLA_MASTER]);
569 if (!if_indextoname(br_idx, br_name)) {
574 if (cfg_lock(NULL, lock_timeout)) {
575 /* Couldn't lock config file. */
576 /* xxx this should try again and print error msg. */
582 cfg_get_all_keys(&ports, "bridge.%s.port", br_name);
584 if (svec_contains(&ports, port_name)) {
585 del_port(br_name, port_name);
586 rewrite_and_reload_config();
595 main(int argc, char *argv[])
597 struct unixctl_server *unixctl;
600 set_program_name(argv[0]);
601 register_fault_handlers();
604 parse_options(argc, argv);
605 signal(SIGPIPE, SIG_IGN);
608 die_if_already_running();
611 retval = unixctl_server_create(NULL, &unixctl);
613 ovs_fatal(retval, "could not listen for vlog connections");
616 if (brc_open(&brc_sock)) {
617 ovs_fatal(0, "could not open brcompat socket. Check "
618 "\"brcompat\" kernel module.");
622 if (nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, &rtnl_sock)) {
623 ovs_fatal(0, "could not create rtnetlink socket");
630 unixctl_server_run(unixctl);
633 /* If 'prune_timeout' is non-zero, we actively prune from the
634 * config file any 'bridge.<br_name>.port' entries that are no
635 * longer valid. We use two methods:
637 * 1) The kernel explicitly notifies us of removed ports
638 * through the RTNL messages.
640 * 2) We periodically check all ports associated with bridges
641 * to see if they no longer exist.
647 nl_sock_wait(rtnl_sock, POLLIN);
648 poll_timer_wait(prune_timeout);
651 nl_sock_wait(brc_sock, POLLIN);
652 unixctl_server_wait(unixctl);
660 parse_options(int argc, char *argv[])
663 OPT_LOCK_TIMEOUT = UCHAR_MAX + 1,
667 LEAK_CHECKER_OPTION_ENUMS
669 static struct option long_options[] = {
670 {"help", no_argument, 0, 'h'},
671 {"version", no_argument, 0, 'V'},
672 {"lock-timeout", required_argument, 0, OPT_LOCK_TIMEOUT},
673 {"prune-timeout", required_argument, 0, OPT_PRUNE_TIMEOUT},
674 {"reload-command", required_argument, 0, OPT_RELOAD_COMMAND},
677 LEAK_CHECKER_LONG_OPTIONS,
680 char *short_options = long_options_to_short_options(long_options);
683 reload_command = xasprintf("%s/ovs-appctl -t "
684 "%s/ovs-vswitchd.`cat %s/ovs-vswitchd.pid`.ctl "
685 "-e vswitchd/reload 2>&1 "
686 "| /usr/bin/logger -t brcompatd-reload",
687 ovs_bindir, ovs_rundir, ovs_rundir);
691 c = getopt_long(argc, argv, short_options, long_options, NULL);
702 OVS_PRINT_VERSION(0, 0);
705 case OPT_LOCK_TIMEOUT:
706 lock_timeout = atoi(optarg);
709 case OPT_PRUNE_TIMEOUT:
710 prune_timeout = atoi(optarg) * 1000;
713 case OPT_RELOAD_COMMAND:
714 reload_command = optarg;
718 DAEMON_OPTION_HANDLERS
719 LEAK_CHECKER_OPTION_HANDLERS
734 ovs_fatal(0, "exactly one non-option argument required; "
735 "use --help for usage");
738 config_file = argv[0];
739 error = cfg_set_file(config_file);
741 ovs_fatal(error, "failed to add configuration file \"%s\"",
749 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
750 "usage: %s [OPTIONS] CONFIG\n"
751 "CONFIG is the configuration file used by ovs-vswitchd.\n",
752 program_name, program_name);
753 printf("\nConfiguration options:\n"
754 " --reload-command=COMMAND shell command to reload ovs-vswitchd\n"
755 " --prune-timeout=SECS wait at most SECS before pruning ports\n"
756 " --lock-timeout=MSECS wait at most MSECS for CONFIG to unlock\n"
760 printf("\nOther options:\n"
761 " -h, --help display this help message\n"
762 " -V, --version display version information\n");
763 leak_checker_usage();
764 printf("\nThe default reload command is:\n%s\n", reload_command);