1 /* Copyright (c) 2008, 2009, 2010, 2011 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>
36 #include "command-line.h"
40 #include "dynamic-string.h"
41 #include "fatal-signal.h"
43 #include "leak-checker.h"
46 #include "netlink-notifier.h"
47 #include "netlink-socket.h"
49 #include "openvswitch/brcompat-netlink.h"
51 #include "poll-loop.h"
53 #include "rtnetlink-link.h"
62 VLOG_DEFINE_THIS_MODULE(brcompatd);
64 /* xxx Just hangs if datapath is rmmod/insmod. Learn to reconnect? */
66 static void parse_options(int argc, char *argv[]);
67 static void usage(void) NO_RETURN;
69 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
71 /* --appctl: Absolute path to ovs-appctl. */
72 static char *appctl_program;
74 /* --vsctl: Absolute path to ovs-vsctl. */
75 static char *vsctl_program;
77 /* Options that we should generally pass to ovs-vsctl. */
78 #define VSCTL_OPTIONS "--timeout=5", "-vANY:console:WARN"
80 /* Netlink socket to bridge compatibility kernel module. */
81 static struct nl_sock *brc_sock;
83 /* The Generic Netlink family number used for bridge compatibility. */
84 static int brc_family;
86 static const struct nl_policy brc_multicast_policy[] = {
87 [BRC_GENL_A_MC_GROUP] = {.type = NL_A_U32 }
91 capture_vsctl_valist(const char *arg0, va_list args)
93 char *stdout_log, *stderr_log;
94 enum vlog_level log_level;
99 /* Compose arguments. */
101 svec_add(&argv, arg0);
103 const char *arg = va_arg(args, const char *);
107 svec_add(&argv, arg);
109 svec_terminate(&argv);
112 if (process_run_capture(argv.names, &stdout_log, &stderr_log, SIZE_MAX,
119 if (WIFEXITED(status)) {
120 int code = WEXITSTATUS(status);
121 log_level = code == 0 ? VLL_DBG : code == 1 ? VLL_WARN : VLL_ERR;
125 msg = process_status_msg(status);
126 VLOG(log_level, "ovs-vsctl exited (%s)", msg);
127 if (stdout_log && *stdout_log) {
128 VLOG(log_level, "ovs-vsctl wrote to stdout:\n%s\n", stdout_log);
130 if (stderr_log && *stderr_log) {
131 VLOG(log_level, "ovs-vsctl wrote to stderr:\n%s\n", stderr_log);
138 if (WIFEXITED(status) && !WEXITSTATUS(status)) {
146 static char * SENTINEL(0)
147 capture_vsctl(const char *arg0, ...)
152 va_start(args, arg0);
153 stdout_log = capture_vsctl_valist(arg0, args);
159 static bool SENTINEL(0)
160 run_vsctl(const char *arg0, ...)
166 va_start(args, arg0);
167 stdout_log = capture_vsctl_valist(arg0, args);
170 ok = stdout_log != NULL;
176 lookup_brc_multicast_group(int *multicast_group)
178 struct nl_sock *sock;
179 struct ofpbuf request, *reply;
180 struct nlattr *attrs[ARRAY_SIZE(brc_multicast_policy)];
183 retval = nl_sock_create(NETLINK_GENERIC, &sock);
187 ofpbuf_init(&request, 0);
188 nl_msg_put_genlmsghdr(&request, 0, brc_family,
189 NLM_F_REQUEST, BRC_GENL_C_QUERY_MC, 1);
190 retval = nl_sock_transact(sock, &request, &reply);
191 ofpbuf_uninit(&request);
193 nl_sock_destroy(sock);
196 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
197 brc_multicast_policy, attrs,
198 ARRAY_SIZE(brc_multicast_policy))) {
199 nl_sock_destroy(sock);
200 ofpbuf_delete(reply);
203 *multicast_group = nl_attr_get_u32(attrs[BRC_GENL_A_MC_GROUP]);
204 nl_sock_destroy(sock);
205 ofpbuf_delete(reply);
210 /* Opens a socket for brcompat notifications. Returns 0 if successful,
211 * otherwise a positive errno value. */
213 brc_open(struct nl_sock **sock)
215 int multicast_group = 0;
218 retval = nl_lookup_genl_family(BRC_GENL_FAMILY_NAME, &brc_family);
223 retval = lookup_brc_multicast_group(&multicast_group);
228 retval = nl_sock_create(NETLINK_GENERIC, sock);
233 retval = nl_sock_join_mcgroup(*sock, multicast_group);
235 nl_sock_destroy(*sock);
241 static const struct nl_policy brc_dp_policy[] = {
242 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
246 parse_command(struct ofpbuf *buffer, uint32_t *seq, const char **br_name,
247 const char **port_name, uint64_t *count, uint64_t *skip)
249 static const struct nl_policy policy[] = {
250 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING, .optional = true },
251 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING, .optional = true },
252 [BRC_GENL_A_FDB_COUNT] = { .type = NL_A_U64, .optional = true },
253 [BRC_GENL_A_FDB_SKIP] = { .type = NL_A_U64, .optional = true },
255 struct nlattr *attrs[ARRAY_SIZE(policy)];
257 if (!nl_policy_parse(buffer, NLMSG_HDRLEN + GENL_HDRLEN, policy,
258 attrs, ARRAY_SIZE(policy))
259 || (br_name && !attrs[BRC_GENL_A_DP_NAME])
260 || (port_name && !attrs[BRC_GENL_A_PORT_NAME])
261 || (count && !attrs[BRC_GENL_A_FDB_COUNT])
262 || (skip && !attrs[BRC_GENL_A_FDB_SKIP])) {
266 *seq = ((struct nlmsghdr *) buffer->data)->nlmsg_seq;
268 *br_name = nl_attr_get_string(attrs[BRC_GENL_A_DP_NAME]);
271 *port_name = nl_attr_get_string(attrs[BRC_GENL_A_PORT_NAME]);
274 *count = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_COUNT]);
277 *skip = nl_attr_get_u64(attrs[BRC_GENL_A_FDB_SKIP]);
282 /* Composes and returns a reply to a request made by the datapath with Netlink
283 * sequence number 'seq' and error code 'error'. The caller may add additional
284 * attributes to the message, then it may send it with send_reply(). */
285 static struct ofpbuf *
286 compose_reply(uint32_t seq, int error)
288 struct ofpbuf *reply = ofpbuf_new(4096);
289 nl_msg_put_genlmsghdr(reply, 32, brc_family, NLM_F_REQUEST,
290 BRC_GENL_C_DP_RESULT, 1);
291 ((struct nlmsghdr *) reply->data)->nlmsg_seq = seq;
292 nl_msg_put_u32(reply, BRC_GENL_A_ERR_CODE, error);
296 /* Sends 'reply' to the datapath and frees it. */
298 send_reply(struct ofpbuf *reply)
300 int retval = nl_sock_send(brc_sock, reply, false);
302 VLOG_WARN_RL(&rl, "replying to brcompat request: %s",
305 ofpbuf_delete(reply);
308 /* Composes and sends a reply to a request made by the datapath with Netlink
309 * sequence number 'seq' and error code 'error'. */
311 send_simple_reply(uint32_t seq, int error)
313 send_reply(compose_reply(seq, error));
317 handle_bridge_cmd(struct ofpbuf *buffer, bool add)
323 error = parse_command(buffer, &seq, &br_name, NULL, NULL, NULL);
325 const char *vsctl_cmd = add ? "add-br" : "del-br";
326 const char *brctl_cmd = add ? "addbr" : "delbr";
327 if (!run_vsctl(vsctl_program, VSCTL_OPTIONS,
328 "--", vsctl_cmd, br_name,
329 "--", "comment", "ovs-brcompatd:", brctl_cmd, br_name,
331 error = add ? EEXIST : ENXIO;
333 send_simple_reply(seq, error);
338 static const struct nl_policy brc_port_policy[] = {
339 [BRC_GENL_A_DP_NAME] = { .type = NL_A_STRING },
340 [BRC_GENL_A_PORT_NAME] = { .type = NL_A_STRING },
344 handle_port_cmd(struct ofpbuf *buffer, bool add)
346 const char *br_name, *port_name;
350 error = parse_command(buffer, &seq, &br_name, &port_name, NULL, NULL);
352 const char *vsctl_cmd = add ? "add-port" : "del-port";
353 const char *brctl_cmd = add ? "addif" : "delif";
354 if (!run_vsctl(vsctl_program, VSCTL_OPTIONS,
355 "--", vsctl_cmd, br_name, port_name,
356 "--", "comment", "ovs-brcompatd:", brctl_cmd,
357 br_name, port_name, (char *) NULL)) {
360 send_simple_reply(seq, error);
366 linux_bridge_to_ovs_bridge(const char *linux_name, int *br_vlanp)
368 char *save_ptr = NULL;
369 const char *br_name, *br_vlan;
373 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS,
374 "--", "br-to-parent", linux_name,
375 "--", "br-to-vlan", linux_name,
381 br_name = strtok_r(output, " \t\r\n", &save_ptr);
382 br_vlan = strtok_r(NULL, " \t\r\n", &save_ptr);
383 if (!br_name || !br_vlan) {
387 br_name_copy = xstrdup(br_name);
388 *br_vlanp = atoi(br_vlan);
396 get_bridge_ifaces(const char *br_name, struct sset *ifaces)
398 char *save_ptr = NULL;
402 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-ifaces",
403 br_name, (char *) NULL);
408 for (iface = strtok_r(output, " \t\r\n", &save_ptr); iface;
409 iface = strtok_r(NULL, " \t\r\n", &save_ptr)) {
410 sset_add(ifaces, iface);
416 handle_fdb_query_cmd(struct ofpbuf *buffer)
418 /* This structure is copied directly from the Linux 2.6.30 header files.
419 * It would be more straightforward to #include <linux/if_bridge.h>, but
420 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
421 * with old header files won't have it. */
426 __u32 ageing_timer_value;
435 struct mac *local_macs;
439 /* Impedance matching between the vswitchd and Linux kernel notions of what
440 * a bridge is. The kernel only handles a single VLAN per bridge, but
441 * vswitchd can deal with all the VLANs on a single bridge. We have to
442 * pretend that the former is the case even though the latter is the
444 const char *linux_name; /* Name used by brctl. */
445 int br_vlan; /* VLAN tag. */
448 struct ofpbuf query_data;
449 const char *iface_name;
450 struct ofpbuf *reply;
451 uint64_t count, skip;
458 /* Parse the command received from brcompat_mod. */
459 error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
464 /* Figure out vswitchd bridge and VLAN. */
465 br_name = linux_bridge_to_ovs_bridge(linux_name, &br_vlan);
468 send_simple_reply(seq, error);
472 /* Fetch the forwarding database using ovs-appctl. */
473 output = capture_vsctl(appctl_program, "fdb/show", br_name,
477 send_simple_reply(seq, error);
481 /* Fetch the MAC address for each interface on the bridge, so that we can
482 * fill in the is_local field in the response. */
484 get_bridge_ifaces(linux_name, &ifaces);
485 local_macs = xmalloc(sset_count(&ifaces) * sizeof *local_macs);
487 SSET_FOR_EACH (iface_name, &ifaces) {
488 struct mac *mac = &local_macs[n_local_macs];
489 struct netdev *netdev;
491 error = netdev_open(iface_name, "system", &netdev);
493 if (!netdev_get_etheraddr(netdev, mac->addr)) {
496 netdev_close(netdev);
499 sset_destroy(&ifaces);
501 /* Parse the response from ovs-appctl and convert it to binary format to
502 * pass back to the kernel. */
503 ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
505 strtok_r(output, "\n", &save_ptr); /* Skip header line. */
507 struct __fdb_entry *entry;
509 uint8_t mac[ETH_ADDR_LEN];
513 line = strtok_r(NULL, "\n", &save_ptr);
518 if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
519 &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
520 != 2 + ETH_ADDR_SCAN_COUNT + 1) {
521 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
522 VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
526 if (vlan != br_vlan) {
535 /* Is this the MAC address of an interface on the bridge? */
537 for (i = 0; i < n_local_macs; i++) {
538 if (eth_addr_equals(local_macs[i].addr, mac)) {
544 entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
545 memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
546 entry->port_no = port & 0xff;
547 entry->is_local = is_local;
548 entry->ageing_timer_value = age * HZ;
549 entry->port_hi = (port & 0xff00) >> 8;
556 /* Compose and send reply to datapath. */
557 reply = compose_reply(seq, 0);
558 nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
559 query_data.data, query_data.size);
563 ofpbuf_uninit(&query_data);
570 send_ifindex_reply(uint32_t seq, char *output)
572 size_t allocated_indices;
573 char *save_ptr = NULL;
574 struct ofpbuf *reply;
580 n_indices = allocated_indices = 0;
581 for (iface = strtok_r(output, " \t\r\n", &save_ptr); iface;
582 iface = strtok_r(NULL, " \t\r\n", &save_ptr)) {
585 if (n_indices >= allocated_indices) {
586 indices = x2nrealloc(indices, &allocated_indices, sizeof *indices);
589 ifindex = if_nametoindex(iface);
591 indices[n_indices++] = ifindex;
595 /* Compose and send reply. */
596 reply = compose_reply(seq, 0);
597 nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
598 indices, n_indices * sizeof *indices);
606 handle_get_bridges_cmd(struct ofpbuf *buffer)
612 /* Parse Netlink command.
614 * The command doesn't actually have any arguments, but we need the
615 * sequence number to send the reply. */
616 error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
621 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-br", (char *) NULL);
626 send_ifindex_reply(seq, output);
632 handle_get_ports_cmd(struct ofpbuf *buffer)
634 const char *linux_name;
639 /* Parse Netlink command. */
640 error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
645 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-ports", linux_name,
651 send_ifindex_reply(seq, output);
656 static struct ofpbuf *
657 brc_recv_update__(void)
660 struct ofpbuf *buffer;
663 retval = nl_sock_recv(brc_sock, &buffer, false);
666 if (nl_msg_nlmsgerr(buffer, NULL)
667 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE) {
679 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
682 ofpbuf_delete(buffer);
687 brc_recv_update(void)
689 struct ofpbuf *buffer;
690 struct genlmsghdr *genlmsghdr;
692 buffer = brc_recv_update__();
697 genlmsghdr = nl_msg_genlmsghdr(buffer);
699 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
703 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
704 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
705 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
709 /* Service all pending network device notifications before executing the
710 * command. This is very important to avoid a race in a scenario like the
711 * following, which is what happens with XenServer Tools version 5.0.0
712 * during boot of a Windows VM:
714 * 1. Create tap1.0 and vif1.0.
717 * 4. Re-create vif1.0.
719 * We must process the network device notification from step 3 before we
720 * process the brctl command from step 4. If we process them in the
721 * reverse order, then step 4 completes as a no-op but step 3 then deletes
722 * the port that was just added.
724 * (XenServer Tools 5.5.0 does not exhibit this behavior, and neither does
725 * a VM without Tools installed at all.)
727 rtnetlink_link_run();
729 switch (genlmsghdr->cmd) {
730 case BRC_GENL_C_DP_ADD:
731 handle_bridge_cmd(buffer, true);
734 case BRC_GENL_C_DP_DEL:
735 handle_bridge_cmd(buffer, false);
738 case BRC_GENL_C_PORT_ADD:
739 handle_port_cmd(buffer, true);
742 case BRC_GENL_C_PORT_DEL:
743 handle_port_cmd(buffer, false);
746 case BRC_GENL_C_FDB_QUERY:
747 handle_fdb_query_cmd(buffer);
750 case BRC_GENL_C_GET_BRIDGES:
751 handle_get_bridges_cmd(buffer);
754 case BRC_GENL_C_GET_PORTS:
755 handle_get_ports_cmd(buffer);
759 VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
765 ofpbuf_delete(buffer);
769 netdev_changed_cb(const struct rtnetlink_link_change *change,
770 void *aux OVS_UNUSED)
772 char br_name[IFNAMSIZ];
773 const char *port_name;
776 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
780 if (change->nlmsg_type != RTM_DELLINK || !change->master_ifindex) {
784 port_name = change->ifname;
785 if (!if_indextoname(change->master_ifindex, br_name)) {
789 VLOG_INFO("network device %s destroyed, removing from bridge %s",
792 run_vsctl(vsctl_program, VSCTL_OPTIONS,
793 "--", "--if-exists", "del-port", port_name,
794 "--", "comment", "ovs-brcompatd:", port_name, "disappeared",
799 main(int argc, char *argv[])
801 extern struct vlog_module VLM_reconnect;
802 struct nln_notifier *link_notifier;
803 struct unixctl_server *unixctl;
806 proctitle_init(argc, argv);
807 set_program_name(argv[0]);
808 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
810 parse_options(argc, argv);
811 signal(SIGPIPE, SIG_IGN);
816 retval = unixctl_server_create(NULL, &unixctl);
821 if (brc_open(&brc_sock)) {
822 VLOG_FATAL("could not open brcompat socket. Check "
823 "\"brcompat\" kernel module.");
826 link_notifier = rtnetlink_link_notifier_create(netdev_changed_cb, NULL);
828 daemonize_complete();
831 unixctl_server_run(unixctl);
832 rtnetlink_link_run();
837 nl_sock_wait(brc_sock, POLLIN);
838 unixctl_server_wait(unixctl);
839 rtnetlink_link_wait();
844 rtnetlink_link_notifier_destroy(link_notifier);
850 parse_options(int argc, char *argv[])
856 LEAK_CHECKER_OPTION_ENUMS,
859 static struct option long_options[] = {
860 {"help", no_argument, NULL, 'h'},
861 {"version", no_argument, NULL, 'V'},
862 {"appctl", required_argument, NULL, OPT_APPCTL},
863 {"vsctl", required_argument, NULL, OPT_VSCTL},
866 LEAK_CHECKER_LONG_OPTIONS,
869 char *short_options = long_options_to_short_options(long_options);
870 const char *appctl = "ovs-appctl";
871 const char *vsctl = "ovs-vsctl";
876 c = getopt_long(argc, argv, short_options, long_options, NULL);
886 ovs_print_version(0, 0);
898 DAEMON_OPTION_HANDLERS
899 LEAK_CHECKER_OPTION_HANDLERS
910 appctl_program = process_search_path(appctl);
911 if (!appctl_program) {
912 VLOG_FATAL("%s: not found in $PATH (use --appctl to specify an "
913 "alternate location)", appctl);
916 vsctl_program = process_search_path(vsctl);
917 if (!vsctl_program) {
918 VLOG_FATAL("%s: not found in $PATH (use --vsctl to specify an "
919 "alternate location)", vsctl);
922 if (argc != optind) {
923 VLOG_FATAL("no non-option arguments are supported; "
924 "use --help for usage");
931 printf("%s: bridge compatibility front-end for ovs-vswitchd\n"
932 "usage: %s [OPTIONS]\n",
933 program_name, program_name);
934 printf("\nConfiguration options:\n"
935 " --appctl=PROGRAM overrides $PATH for finding ovs-appctl\n"
936 " --vsctl=PROGRAM overrides $PATH for finding ovs-vsctl\n"
940 printf("\nOther options:\n"
941 " -h, --help display this help message\n"
942 " -V, --version display version information\n");
943 leak_checker_usage();