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-socket.h"
48 #include "openvswitch/brcompat-netlink.h"
50 #include "poll-loop.h"
52 #include "rtnetlink.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)
368 char *save_ptr = NULL;
373 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "br-to-parent",
374 linux_name, (char *) NULL);
379 br_name = strtok_r(output, " \t\r\n", &save_ptr);
384 br_name_copy = xstrdup(br_name);
392 get_bridge_ifaces(const char *br_name, struct sset *ifaces)
394 char *save_ptr = NULL;
398 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-ifaces",
399 br_name, (char *) NULL);
404 for (iface = strtok_r(output, " \t\r\n", &save_ptr); iface;
405 iface = strtok_r(NULL, " \t\r\n", &save_ptr)) {
406 sset_add(ifaces, iface);
412 handle_fdb_query_cmd(struct ofpbuf *buffer)
414 /* This structure is copied directly from the Linux 2.6.30 header files.
415 * It would be more straightforward to #include <linux/if_bridge.h>, but
416 * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
417 * with old header files won't have it. */
422 __u32 ageing_timer_value;
431 struct mac *local_macs;
435 /* Impedance matching between the vswitchd and Linux kernel notions of what
436 * a bridge is. The kernel only handles a single VLAN per bridge, but
437 * vswitchd can deal with all the VLANs on a single bridge. We have to
438 * pretend that the former is the case even though the latter is the
440 const char *linux_name; /* Name used by brctl. */
441 int br_vlan; /* VLAN tag. */
444 struct ofpbuf query_data;
445 const char *iface_name;
446 struct ofpbuf *reply;
447 uint64_t count, skip;
454 /* Parse the command received from brcompat_mod. */
455 error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
460 /* Figure out vswitchd bridge and VLAN. */
461 br_name = linux_bridge_to_ovs_bridge(linux_name);
464 send_simple_reply(seq, error);
468 /* Fetch the forwarding database using ovs-appctl. */
469 output = capture_vsctl(appctl_program, "fdb/show", br_name,
473 send_simple_reply(seq, error);
477 /* Fetch the MAC address for each interface on the bridge, so that we can
478 * fill in the is_local field in the response. */
480 get_bridge_ifaces(linux_name, &ifaces);
481 local_macs = xmalloc(sset_count(&ifaces) * sizeof *local_macs);
483 SSET_FOR_EACH (iface_name, &ifaces) {
484 struct mac *mac = &local_macs[n_local_macs];
485 struct netdev *netdev;
487 error = netdev_open_default(iface_name, &netdev);
489 if (!netdev_get_etheraddr(netdev, mac->addr)) {
492 netdev_close(netdev);
495 sset_destroy(&ifaces);
497 /* Parse the response from ovs-appctl and convert it to binary format to
498 * pass back to the kernel. */
499 ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
501 strtok_r(output, "\n", &save_ptr); /* Skip header line. */
503 struct __fdb_entry *entry;
505 uint8_t mac[ETH_ADDR_LEN];
509 line = strtok_r(NULL, "\n", &save_ptr);
514 if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
515 &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
516 != 2 + ETH_ADDR_SCAN_COUNT + 1) {
517 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
518 VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
522 if (vlan != br_vlan) {
531 /* Is this the MAC address of an interface on the bridge? */
533 for (i = 0; i < n_local_macs; i++) {
534 if (eth_addr_equals(local_macs[i].addr, mac)) {
540 entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
541 memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
542 entry->port_no = port & 0xff;
543 entry->is_local = is_local;
544 entry->ageing_timer_value = age * HZ;
545 entry->port_hi = (port & 0xff00) >> 8;
552 /* Compose and send reply to datapath. */
553 reply = compose_reply(seq, 0);
554 nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
555 query_data.data, query_data.size);
559 ofpbuf_uninit(&query_data);
566 send_ifindex_reply(uint32_t seq, char *output)
568 size_t allocated_indices;
569 char *save_ptr = NULL;
570 struct ofpbuf *reply;
576 n_indices = allocated_indices = 0;
577 for (iface = strtok_r(output, " \t\r\n", &save_ptr); iface;
578 iface = strtok_r(NULL, " \t\r\n", &save_ptr)) {
581 if (n_indices >= allocated_indices) {
582 indices = x2nrealloc(indices, &allocated_indices, sizeof *indices);
585 ifindex = if_nametoindex(iface);
587 indices[n_indices++] = ifindex;
591 /* Compose and send reply. */
592 reply = compose_reply(seq, 0);
593 nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
594 indices, n_indices * sizeof *indices);
602 handle_get_bridges_cmd(struct ofpbuf *buffer)
608 /* Parse Netlink command.
610 * The command doesn't actually have any arguments, but we need the
611 * sequence number to send the reply. */
612 error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
617 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-br", (char *) NULL);
622 send_ifindex_reply(seq, output);
628 handle_get_ports_cmd(struct ofpbuf *buffer)
630 const char *linux_name;
635 /* Parse Netlink command. */
636 error = parse_command(buffer, &seq, &linux_name, NULL, NULL, NULL);
641 output = capture_vsctl(vsctl_program, VSCTL_OPTIONS, "list-ports", linux_name,
647 send_ifindex_reply(seq, output);
652 static struct ofpbuf *
653 brc_recv_update__(void)
656 struct ofpbuf *buffer;
659 retval = nl_sock_recv(brc_sock, &buffer, false);
662 if (nl_msg_nlmsgerr(buffer, NULL)
663 || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE) {
675 VLOG_WARN_RL(&rl, "brc_recv_update: %s", strerror(retval));
678 ofpbuf_delete(buffer);
683 brc_recv_update(void)
685 struct ofpbuf *buffer;
686 struct genlmsghdr *genlmsghdr;
688 buffer = brc_recv_update__();
693 genlmsghdr = nl_msg_genlmsghdr(buffer);
695 VLOG_WARN_RL(&rl, "received packet too short for generic NetLink");
699 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != brc_family) {
700 VLOG_DBG_RL(&rl, "received type (%"PRIu16") != brcompat family (%d)",
701 nl_msg_nlmsghdr(buffer)->nlmsg_type, brc_family);
705 /* Service all pending network device notifications before executing the
706 * command. This is very important to avoid a race in a scenario like the
707 * following, which is what happens with XenServer Tools version 5.0.0
708 * during boot of a Windows VM:
710 * 1. Create tap1.0 and vif1.0.
713 * 4. Re-create vif1.0.
715 * We must process the network device notification from step 3 before we
716 * process the brctl command from step 4. If we process them in the
717 * reverse order, then step 4 completes as a no-op but step 3 then deletes
718 * the port that was just added.
720 * (XenServer Tools 5.5.0 does not exhibit this behavior, and neither does
721 * a VM without Tools installed at all.)
723 rtnetlink_link_notifier_run();
725 switch (genlmsghdr->cmd) {
726 case BRC_GENL_C_DP_ADD:
727 handle_bridge_cmd(buffer, true);
730 case BRC_GENL_C_DP_DEL:
731 handle_bridge_cmd(buffer, false);
734 case BRC_GENL_C_PORT_ADD:
735 handle_port_cmd(buffer, true);
738 case BRC_GENL_C_PORT_DEL:
739 handle_port_cmd(buffer, false);
742 case BRC_GENL_C_FDB_QUERY:
743 handle_fdb_query_cmd(buffer);
746 case BRC_GENL_C_GET_BRIDGES:
747 handle_get_bridges_cmd(buffer);
750 case BRC_GENL_C_GET_PORTS:
751 handle_get_ports_cmd(buffer);
755 VLOG_WARN_RL(&rl, "received unknown brc netlink command: %d\n",
761 ofpbuf_delete(buffer);
765 netdev_changed_cb(const struct rtnetlink_link_change *change,
766 void *aux OVS_UNUSED)
768 char br_name[IFNAMSIZ];
769 const char *port_name;
772 VLOG_WARN_RL(&rl, "network monitor socket overflowed");
776 if (change->nlmsg_type != RTM_DELLINK || !change->master_ifindex) {
780 port_name = change->ifname;
781 if (!if_indextoname(change->master_ifindex, br_name)) {
785 VLOG_INFO("network device %s destroyed, removing from bridge %s",
788 run_vsctl(vsctl_program, VSCTL_OPTIONS,
789 "--", "--if-exists", "del-port", br_name, port_name,
790 "--", "comment", "ovs-brcompatd:", port_name, "disappeared",
795 main(int argc, char *argv[])
797 extern struct vlog_module VLM_reconnect;
798 struct rtnetlink_notifier link_notifier;
799 struct unixctl_server *unixctl;
802 proctitle_init(argc, argv);
803 set_program_name(argv[0]);
804 vlog_set_levels(&VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
806 parse_options(argc, argv);
807 signal(SIGPIPE, SIG_IGN);
812 retval = unixctl_server_create(NULL, &unixctl);
817 if (brc_open(&brc_sock)) {
818 VLOG_FATAL("could not open brcompat socket. Check "
819 "\"brcompat\" kernel module.");
823 rtnetlink_link_notifier_register(&link_notifier, netdev_changed_cb, NULL);
825 daemonize_complete();
828 unixctl_server_run(unixctl);
829 rtnetlink_link_notifier_run();
834 nl_sock_wait(brc_sock, POLLIN);
835 unixctl_server_wait(unixctl);
836 rtnetlink_link_notifier_wait();
841 rtnetlink_link_notifier_unregister(&link_notifier);
847 parse_options(int argc, char *argv[])
853 LEAK_CHECKER_OPTION_ENUMS,
856 static struct option long_options[] = {
857 {"help", no_argument, NULL, 'h'},
858 {"version", no_argument, NULL, 'V'},
859 {"appctl", required_argument, NULL, OPT_APPCTL},
860 {"vsctl", required_argument, NULL, OPT_VSCTL},
863 LEAK_CHECKER_LONG_OPTIONS,
866 char *short_options = long_options_to_short_options(long_options);
867 const char *appctl = "ovs-appctl";
868 const char *vsctl = "ovs-vsctl";
873 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();