1 /* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
18 #include <asm/param.h>
25 #include <linux/genetlink.h>
26 #include <linux/rtnetlink.h>
30 #include <sys/types.h>
37 #include "command-line.h"
41 #include "dynamic-string.h"
42 #include "fatal-signal.h"
44 #include "leak-checker.h"
47 #include "netlink-notifier.h"
48 #include "netlink-socket.h"
50 #include "openvswitch/brcompat-netlink.h"
52 #include "poll-loop.h"
54 #include "rtnetlink-link.h"
63 VLOG_DEFINE_THIS_MODULE(brcompatd);
65 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
67 static void parse_options(int argc, char *argv[]);
68 static void usage(void) NO_RETURN;
70 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
72 /* --appctl: Absolute path to ovs-appctl. */
73 static char *appctl_program;
75 /* --vsctl: Absolute path to ovs-vsctl. */
76 static char *vsctl_program;
78 /* Options that we should generally pass to ovs-vsctl. */
79 #define VSCTL_OPTIONS "--timeout=5", "-vANY:console:WARN"
81 /* Netlink socket to bridge compatibility kernel module. */
82 static struct nl_sock *brc_sock;
84 /* The Generic Netlink family number used for bridge compatibility. */
85 static int brc_family;
87 static const struct nl_policy brc_multicast_policy[] = {
88 [BRC_GENL_A_MC_GROUP] = {.type = NL_A_U32 }
92 capture_vsctl_valist(const char *arg0, va_list args)
94 char *stdout_log, *stderr_log;
95 enum vlog_level log_level;
100 /* Compose arguments. */
102 svec_add(&argv, arg0);
104 const char *arg = va_arg(args, const char *);
108 svec_add(&argv, arg);
110 svec_terminate(&argv);
113 if (process_run_capture(argv.names, &stdout_log, &stderr_log, SIZE_MAX,
120 if (WIFEXITED(status)) {
121 int code = WEXITSTATUS(status);
122 log_level = code == 0 ? VLL_DBG : code == 1 ? VLL_WARN : VLL_ERR;
126 msg = process_status_msg(status);
127 VLOG(log_level, "ovs-vsctl exited (%s)", msg);
128 if (stdout_log && *stdout_log) {
129 VLOG(log_level, "ovs-vsctl wrote to stdout:\n%s\n", stdout_log);
131 if (stderr_log && *stderr_log) {
132 VLOG(log_level, "ovs-vsctl wrote to stderr:\n%s\n", stderr_log);
139 if (WIFEXITED(status) && !WEXITSTATUS(status)) {
147 static char * SENTINEL(0)
148 capture_vsctl(const char *arg0, ...)
153 va_start(args, arg0);
154 stdout_log = capture_vsctl_valist(arg0, args);
160 static bool SENTINEL(0)
161 run_vsctl(const char *arg0, ...)
167 va_start(args, arg0);
168 stdout_log = capture_vsctl_valist(arg0, args);
171 ok = stdout_log != NULL;
177 lookup_brc_multicast_group(int *multicast_group)
179 struct nl_sock *sock;
180 struct ofpbuf request, *reply;
181 struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
184 retval = nl_sock_create(NETLINK_GENERIC, &sock);
188 ofpbuf_init(&request, 0);
189 nl_msg_put_genlmsghdr(&request, 0, brc_family,
190 NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
191 retval = nl_sock_transact(sock, &request, &reply);
192 ofpbuf_uninit(&request);
194 nl_sock_destroy(sock);
197 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
198 brc_multicast_policy, attrs,
199 ARRAY_SIZE(brc_multicast_policy))) {
200 nl_sock_destroy(sock);
201 ofpbuf_delete(reply);
204 *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
205 nl_sock_destroy(sock);
206 ofpbuf_delete(reply);
211 /* Opens a socket for brcompat notifications. Returns 0 if successful,
212 * otherwise a positive errno value. */
214 brc_open(struct nl_sock **sock)
216 int multicast_group = 0;
219 retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
224 retval = lookup_brc_multicast_group(&multicast_group);
229 retval = nl_sock_create(NETLINK_GENERIC, sock);
234 retval = nl_sock_join_mcgroup(*sock, multicast_group);
236 nl_sock_destroy(*sock);
242 static const struct nl_policy brc_dp_policy[] = {
243 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
247 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
248 const char **port_name, uint64_t *count, uint64_t *skip)
250 static const struct nl_policy policy[] = {
251 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING, .optional = true },
252 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
253 [BRC_GENL_A_FDB_COUNT] = { .type = NL_A_U64, .optional = true },
254 [BRC_GENL_A_FDB_SKIP] = { .type = NL_A_U64, .optional = true },
256 struct nlattr *attrs[ARRAY_SIZE(policy)];
258 if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
259 attrs, ARRAY_SIZE(policy))
260 || (br_name && !attrs[BRC_GENL_A_DP_NAME])
261 || (port_name && !attrs[BRC_GENL_A_PORT_NAME])
262 || (count && !attrs[BRC_GENL_A_FDB_COUNT])
263 || (skip && !attrs[BRC_GENL_A_FDB_SKIP])) {
267 *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
269 *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
272 *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
275 *count = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_COUNT]);
278 *skip = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_SKIP]);
283 /* Composes and returns a reply to a request made by the datapath with Netlink
284 * sequence number 'seq' and error code 'error'. The caller may add additional
285 * attributes to the message, then it may send it with send_reply(). */
286 static struct ofpbuf *
287 compose_reply(uint32_t seq, int error)
289 struct ofpbuf *reply = ofpbuf_new(4096);
290 nl_msg_put_genlmsghdr(reply, 32, brc_family, NLM_F_REQUEST,
291 BRC_GENL_C_DP_RESULT, 1);
292 ((struct nlmsghdr *) reply->data)->nlmsg_seq = seq;
293 nl_msg_put_u32(reply, BRC_GENL_A_ERR_CODE, error);
297 /* Sends 'reply' to the datapath and frees it. */
299 send_reply(struct ofpbuf *reply)
301 int retval = nl_sock_send(brc_sock, reply, false);
303 VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
306 ofpbuf_delete(reply);
309 /* Composes and sends a reply to a request made by the datapath with Netlink
310 * sequence number 'seq' and error code 'error'. */
312 send_simple_reply(uint32_t seq, int error)
314 send_reply(compose_reply(seq, error));
318 handle_bridge_cmd(struct ofpbuf *buffer, bool add)
324 error = parse_command(buffer, &seq, &br_name, NULL, NULL, NULL);
326 const char *vsctl_cmd = add ? "add-br" : "del-br";
327 const char *brctl_cmd = add ? "addbr" : "delbr";
328 if (!run_vsctl(vsctl_program, VSCTL_OPTIONS,
329 "--", vsctl_cmd, br_name,
330 "--", "comment", "ovs-brcompatd:", brctl_cmd, br_name,
332 error = add ? EEXIST : ENXIO;
334 send_simple_reply(seq, error);
339 static const struct nl_policy brc_port_policy[] = {
340 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
341 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
345 handle_port_cmd(struct ofpbuf *buffer, bool add)
347 const char *br_name, *port_name;
351 error = parse_command(buffer, &seq, &br_name, &port_name, NULL, NULL);
353 const char *vsctl_cmd = add ? "add-port" : "del-port";
354 const char *brctl_cmd = add ? "addif" : "delif";
355 if (!run_vsctl(vsctl_program, VSCTL_OPTIONS,
356 "--", vsctl_cmd, br_name, port_name,
357 "--", "comment", "ovs-brcompatd:", brctl_cmd,
358 br_name, port_name, (char *) NULL)) {
361 send_simple_reply(seq, error);
367 linux_bridge_to_ovs_bridge(const char *linux_name, int *br_vlanp)
369 char *save_ptr = NULL;
370 const char *br_name, *br_vlan;
374 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS,
375 "--", "br-to-parent", linux_name,
376 "--", "br-to-vlan", linux_name,
382 br_name = strtok_r(output, " \t\r\n", &save_ptr);
383 br_vlan = strtok_r(NULL, " \t\r\n", &save_ptr);
384 if (!br_name || !br_vlan) {
388 br_name_copy = xstrdup(br_name);
389 *br_vlanp = atoi(br_vlan);
397 get_bridge_ifaces(const char *br_name, struct sset *ifaces)
399 char *save_ptr = NULL;
403 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-ifaces",
404 br_name, (char *) NULL);
409 for (iface = strtok_r(output, " \t\r\n", &save_ptr); iface;
410 iface = strtok_r(NULL, " \t\r\n", &save_ptr)) {
411 sset_add(ifaces, iface);
417 handle_fdb_query_cmd(struct ofpbuf *buffer)
419 /* This structure is copied directly from the Linux 2.6.30 header files.
420 * It would be more straightforward to #include <linux/if_bridge.h>, but
421 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
422 * with old header files won't have it. */
427 __u32 ageing_timer_value;
436 struct mac *local_macs;
440 /* Impedance matching between the vswitchd and Linux kernel notions of what
441 * a bridge is. The kernel only handles a single VLAN per bridge, but
442 * vswitchd can deal with all the VLANs on a single bridge. We have to
443 * pretend that the former is the case even though the latter is the
445 const char *linux_name; /* Name used by brctl. */
446 int br_vlan; /* VLAN tag. */
449 struct ofpbuf query_data;
450 const char *iface_name;
451 struct ofpbuf *reply;
452 uint64_t count, skip;
459 /* Parse the command received from brcompat. */
460 error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
465 /* Figure out vswitchd bridge and VLAN. */
466 br_name = linux_bridge_to_ovs_bridge(linux_name, &br_vlan);
469 send_simple_reply(seq, error);
473 /* Fetch the forwarding database using ovs-appctl. */
474 output = capture_vsctl(appctl_program, "fdb/show", br_name,
478 send_simple_reply(seq, error);
482 /* Fetch the MAC address for each interface on the bridge, so that we can
483 * fill in the is_local field in the response. */
485 get_bridge_ifaces(linux_name, &ifaces);
486 local_macs = xmalloc(sset_count(&ifaces) * sizeof *local_macs);
488 SSET_FOR_EACH (iface_name, &ifaces) {
489 struct mac *mac = &local_macs[n_local_macs];
490 struct netdev *netdev;
492 error = netdev_open(iface_name, "system", &netdev);
494 if (!netdev_get_etheraddr(netdev, mac->addr)) {
497 netdev_close(netdev);
500 sset_destroy(&ifaces);
502 /* Parse the response from ovs-appctl and convert it to binary format to
503 * pass back to the kernel. */
504 ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
506 strtok_r(output, "\n", &save_ptr); /* Skip header line. */
508 struct __fdb_entry *entry;
510 uint8_t mac[ETH_ADDR_LEN];
514 line = strtok_r(NULL, "\n", &save_ptr);
519 if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
520 &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
521 != 2 + ETH_ADDR_SCAN_COUNT + 1) {
522 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
523 VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
527 if (vlan != br_vlan) {
536 /* Is this the MAC address of an interface on the bridge? */
538 for (i = 0; i < n_local_macs; i++) {
539 if (eth_addr_equals(local_macs[i].addr, mac)) {
545 entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
546 memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
547 entry->port_no = port & 0xff;
548 entry->is_local = is_local;
549 entry->ageing_timer_value = age * HZ;
550 entry->port_hi = (port & 0xff00) >> 8;
557 /* Compose and send reply to datapath. */
558 reply = compose_reply(seq, 0);
559 nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
560 query_data.data, query_data.size);
564 ofpbuf_uninit(&query_data);
571 send_ifindex_reply(uint32_t seq, char *output)
573 size_t allocated_indices;
574 char *save_ptr = NULL;
575 struct ofpbuf *reply;
581 n_indices = allocated_indices = 0;
582 for (iface = strtok_r(output, " \t\r\n", &save_ptr); iface;
583 iface = strtok_r(NULL, " \t\r\n", &save_ptr)) {
586 if (n_indices >= allocated_indices) {
587 indices = x2nrealloc(indices, &allocated_indices, sizeof *indices);
590 ifindex = if_nametoindex(iface);
592 indices[n_indices++] = ifindex;
596 /* Compose and send reply. */
597 reply = compose_reply(seq, 0);
598 nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
599 indices, n_indices * sizeof *indices);
607 handle_get_bridges_cmd(struct ofpbuf *buffer)
613 /* Parse Netlink command.
615 * The command doesn't actually have any arguments, but we need the
616 * sequence number to send the reply. */
617 error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
622 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-br", (char *) NULL);
627 send_ifindex_reply(seq, output);
633 handle_get_ports_cmd(struct ofpbuf *buffer)
635 const char *linux_name;
640 /* Parse Netlink command. */
641 error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
646 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-ports", linux_name,
652 send_ifindex_reply(seq, output);
658 brc_recv_update__(struct ofpbuf *buffer)
661 int retval = nl_sock_recv(brc_sock, buffer, false);
664 if (nl_msg_nlmsgerr(buffer, NULL)
665 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE) {
677 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
684 brc_recv_update(void)
686 struct genlmsghdr *genlmsghdr;
687 uint64_t buffer_stub[1024 / 8];
688 struct ofpbuf buffer;
690 ofpbuf_use_stub(&buffer, buffer_stub, sizeof buffer_stub);
691 if (!brc_recv_update__(&buffer)) {
695 genlmsghdr = nl_msg_genlmsghdr(&buffer);
697 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
701 if (nl_msg_nlmsghdr(&buffer)->nlmsg_type != brc_family) {
702 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
703 nl_msg_nlmsghdr(&buffer)->nlmsg_type, brc_family);
707 /* Service all pending network device notifications before executing the
708 * command. This is very important to avoid a race in a scenario like the
709 * following, which is what happens with XenServer Tools version 5.0.0
710 * during boot of a Windows VM:
712 * 1. Create tap1.0 and vif1.0.
715 * 4. Re-create vif1.0.
717 * We must process the network device notification from step 3 before we
718 * process the brctl command from step 4. If we process them in the
719 * reverse order, then step 4 completes as a no-op but step 3 then deletes
720 * the port that was just added.
722 * (XenServer Tools 5.5.0 does not exhibit this behavior, and neither does
723 * a VM without Tools installed at all.)
725 rtnetlink_link_run();
727 switch (genlmsghdr->cmd) {
728 case BRC_GENL_C_DP_ADD:
729 handle_bridge_cmd(&buffer, true);
732 case BRC_GENL_C_DP_DEL:
733 handle_bridge_cmd(&buffer, false);
736 case BRC_GENL_C_PORT_ADD:
737 handle_port_cmd(&buffer, true);
740 case BRC_GENL_C_PORT_DEL:
741 handle_port_cmd(&buffer, false);
744 case BRC_GENL_C_FDB_QUERY:
745 handle_fdb_query_cmd(&buffer);
748 case BRC_GENL_C_GET_BRIDGES:
749 handle_get_bridges_cmd(&buffer);
752 case BRC_GENL_C_GET_PORTS:
753 handle_get_ports_cmd(&buffer);
757 VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
763 ofpbuf_uninit(&buffer);
767 netdev_changed_cb(const struct rtnetlink_link_change *change,
768 void *aux OVS_UNUSED)
770 char br_name[IFNAMSIZ];
771 const char *port_name;
774 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
778 if (change->nlmsg_type != RTM_DELLINK || !change->master_ifindex) {
782 port_name = change->ifname;
783 if (!if_indextoname(change->master_ifindex, br_name)) {
787 VLOG_INFO("network device %s destroyed, removing from bridge %s",
790 run_vsctl(vsctl_program, VSCTL_OPTIONS,
791 "--", "--if-exists", "del-port", port_name,
792 "--", "comment", "ovs-brcompatd:", port_name, "disappeared",
797 main(int argc, char *argv[])
799 extern struct vlog_module VLM_reconnect;
800 struct nln_notifier *link_notifier;
801 struct unixctl_server *unixctl;
804 proctitle_init(argc, argv);
805 set_program_name(argv[0]);
806 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
808 parse_options(argc, argv);
809 signal(SIGPIPE, SIG_IGN);
814 retval = unixctl_server_create(NULL, &unixctl);
819 if (brc_open(&brc_sock)) {
820 VLOG_FATAL("could not open brcompat socket. Check "
821 "\"brcompat\" kernel module.");
824 link_notifier = rtnetlink_link_notifier_create(netdev_changed_cb, NULL);
826 daemonize_complete();
829 unixctl_server_run(unixctl);
830 rtnetlink_link_run();
835 nl_sock_wait(brc_sock, POLLIN);
836 unixctl_server_wait(unixctl);
837 rtnetlink_link_wait();
842 rtnetlink_link_notifier_destroy(link_notifier);
848 parse_options(int argc, char *argv[])
854 LEAK_CHECKER_OPTION_ENUMS,
857 static struct option long_options[] = {
858 {"help", no_argument, NULL, 'h'},
859 {"version", no_argument, NULL, 'V'},
860 {"appctl", required_argument, NULL, OPT_APPCTL},
861 {"vsctl", required_argument, NULL, OPT_VSCTL},
864 LEAK_CHECKER_LONG_OPTIONS,
867 char *short_options = long_options_to_short_options(long_options);
868 const char *appctl = "ovs-appctl";
869 const char *vsctl = "ovs-vsctl";
874 c = getopt_long(argc, argv, short_options, long_options, NULL);
884 ovs_print_version(0, 0);
896 DAEMON_OPTION_HANDLERS
897 LEAK_CHECKER_OPTION_HANDLERS
908 appctl_program = process_search_path(appctl);
909 if (!appctl_program) {
910 VLOG_FATAL("%s: not found in $PATH (use --appctl to specify an "
911 "alternate location)", appctl);
914 vsctl_program = process_search_path(vsctl);
915 if (!vsctl_program) {
916 VLOG_FATAL("%s: not found in $PATH (use --vsctl to specify an "
917 "alternate location)", vsctl);
920 if (argc != optind) {
921 VLOG_FATAL("no non-option arguments are supported; "
922 "use --help for usage");
929 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
930 "usage: %s [OPTIONS]\n",
931 program_name, program_name);
932 printf("\nConfiguration options:\n"
933 " --appctl=PROGRAM overrides $PATH for finding ovs-appctl\n"
934 " --vsctl=PROGRAM overrides $PATH for finding ovs-vsctl\n"
938 printf("\nOther options:\n"
939 " -h, --help display this help message\n"
940 " -V, --version display version information\n");
941 leak_checker_usage();