+/* The caller is responsible for freeing '*ovs_name' if the call is
+ * successful. */
+static int
+linux_bridge_to_ovs_bridge(const struct ovsrec_open_vswitch *ovs,
+ const char *linux_name,
+ const struct ovsrec_bridge **ovs_bridge,
+ int *br_vlan)
+{
+ *ovs_bridge = find_bridge(ovs, linux_name);
+ if (*ovs_bridge) {
+ /* Bridge name is the same. We are interested in VLAN 0. */
+ *br_vlan = 0;
+ return 0;
+ } else {
+ /* No such Open vSwitch bridge 'linux_name', but there might be an
+ * internal port named 'linux_name' on some other bridge
+ * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
+ * port 'linux_name' on the bridge named 'ovs_bridge'. */
+ size_t i, j;
+
+ for (i = 0; i < ovs->n_bridges; i++) {
+ const struct ovsrec_bridge *br = ovs->bridges[i];
+
+ for (j = 0; j < br->n_ports; j++) {
+ const struct ovsrec_port *port = br->ports[j];
+
+ if (!strcmp(port->name, linux_name)) {
+ *ovs_bridge = br;
+ *br_vlan = port->n_tag ? *port->tag : -1;
+ return 0;
+ }
+ }
+
+ }
+ return ENODEV;
+ }
+}
+
+static int
+handle_fdb_query_cmd(const struct ovsrec_open_vswitch *ovs,
+ struct ofpbuf *buffer)
+{
+ /* This structure is copied directly from the Linux 2.6.30 header files.
+ * It would be more straightforward to #include <linux/if_bridge.h>, but
+ * the 'port_hi' member was only introduced in Linux 2.6.26 and so systems
+ * with old header files won't have it. */
+ struct __fdb_entry {
+ __u8 mac_addr[6];
+ __u8 port_no;
+ __u8 is_local;
+ __u32 ageing_timer_value;
+ __u8 port_hi;
+ __u8 pad0;
+ __u16 unused;
+ };
+
+ struct mac {
+ uint8_t addr[6];
+ };
+ struct mac *local_macs;
+ int n_local_macs;
+ int i;
+
+ /* Impedance matching between the vswitchd and Linux kernel notions of what
+ * a bridge is. The kernel only handles a single VLAN per bridge, but
+ * vswitchd can deal with all the VLANs on a single bridge. We have to
+ * pretend that the former is the case even though the latter is the
+ * implementation. */
+ const char *linux_name; /* Name used by brctl. */
+ const struct ovsrec_bridge *ovs_bridge; /* Bridge used by ovs-vswitchd. */
+ int br_vlan; /* VLAN tag. */
+ struct svec ifaces;
+
+ struct ofpbuf query_data;
+ struct ofpbuf *reply;
+ char *unixctl_command;
+ uint64_t count, skip;
+ char *output;
+ char *save_ptr;
+ uint32_t seq;
+ int error;
+
+ /* Parse the command received from brcompat_mod. */
+ error = parse_command(buffer, &seq, &linux_name, NULL, &count, &skip);
+ if (error) {
+ return error;
+ }
+
+ /* Figure out vswitchd bridge and VLAN. */
+ error = linux_bridge_to_ovs_bridge(ovs, linux_name,
+ &ovs_bridge, &br_vlan);
+ if (error) {
+ send_simple_reply(seq, error);
+ return error;
+ }
+
+ /* Fetch the forwarding database using ovs-appctl. */
+ unixctl_command = xasprintf("fdb/show %s", ovs_bridge->name);
+ error = execute_appctl_command(unixctl_command, &output);
+ free(unixctl_command);
+ if (error) {
+ send_simple_reply(seq, error);
+ return error;
+ }
+
+ /* Fetch the MAC address for each interface on the bridge, so that we can
+ * fill in the is_local field in the response. */
+ svec_init(&ifaces);
+ get_bridge_ifaces(ovs_bridge, &ifaces, br_vlan);
+ local_macs = xmalloc(ifaces.n * sizeof *local_macs);
+ n_local_macs = 0;
+ for (i = 0; i < ifaces.n; i++) {
+ const char *iface_name = ifaces.names[i];
+ struct mac *mac = &local_macs[n_local_macs];
+ struct netdev *netdev;
+
+ error = netdev_open_default(iface_name, &netdev);
+ if (!error) {
+ if (!netdev_get_etheraddr(netdev, mac->addr)) {
+ n_local_macs++;
+ }
+ netdev_close(netdev);
+ }
+ }
+ svec_destroy(&ifaces);
+
+ /* Parse the response from ovs-appctl and convert it to binary format to
+ * pass back to the kernel. */
+ ofpbuf_init(&query_data, sizeof(struct __fdb_entry) * 8);
+ save_ptr = NULL;
+ strtok_r(output, "\n", &save_ptr); /* Skip header line. */
+ while (count > 0) {
+ struct __fdb_entry *entry;
+ int port, vlan, age;
+ uint8_t mac[ETH_ADDR_LEN];
+ char *line;
+ bool is_local;
+
+ line = strtok_r(NULL, "\n", &save_ptr);
+ if (!line) {
+ break;
+ }
+
+ if (sscanf(line, "%d %d "ETH_ADDR_SCAN_FMT" %d",
+ &port, &vlan, ETH_ADDR_SCAN_ARGS(mac), &age)
+ != 2 + ETH_ADDR_SCAN_COUNT + 1) {
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+ VLOG_INFO_RL(&rl, "fdb/show output has invalid format: %s", line);
+ continue;
+ }
+
+ if (vlan != br_vlan) {
+ continue;
+ }
+
+ if (skip > 0) {
+ skip--;
+ continue;
+ }
+
+ /* Is this the MAC address of an interface on the bridge? */
+ is_local = false;
+ for (i = 0; i < n_local_macs; i++) {
+ if (eth_addr_equals(local_macs[i].addr, mac)) {
+ is_local = true;
+ break;
+ }
+ }
+
+ entry = ofpbuf_put_uninit(&query_data, sizeof *entry);
+ memcpy(entry->mac_addr, mac, ETH_ADDR_LEN);
+ entry->port_no = port & 0xff;
+ entry->is_local = is_local;
+ entry->ageing_timer_value = age * HZ;
+ entry->port_hi = (port & 0xff00) >> 8;
+ entry->pad0 = 0;
+ entry->unused = 0;
+ count--;
+ }
+ free(output);
+
+ /* Compose and send reply to datapath. */
+ reply = compose_reply(seq, 0);
+ nl_msg_put_unspec(reply, BRC_GENL_A_FDB_DATA,
+ query_data.data, query_data.size);
+ send_reply(reply);
+
+ /* Free memory. */
+ ofpbuf_uninit(&query_data);
+
+ return 0;
+}
+
+static void
+send_ifindex_reply(uint32_t seq, struct svec *ifaces)
+{
+ struct ofpbuf *reply;
+ const char *iface;
+ size_t n_indices;
+ int *indices;
+ size_t i;
+
+ /* Make sure that any given interface only occurs once. This shouldn't
+ * happen, but who knows what people put into their configuration files. */
+ svec_sort_unique(ifaces);
+
+ /* Convert 'ifaces' into ifindexes. */
+ n_indices = 0;
+ indices = xmalloc(ifaces->n * sizeof *indices);
+ SVEC_FOR_EACH (i, iface, ifaces) {
+ int ifindex = if_nametoindex(iface);
+ if (ifindex) {
+ indices[n_indices++] = ifindex;
+ }
+ }
+
+ /* Compose and send reply. */
+ reply = compose_reply(seq, 0);
+ nl_msg_put_unspec(reply, BRC_GENL_A_IFINDEXES,
+ indices, n_indices * sizeof *indices);
+ send_reply(reply);
+
+ /* Free memory. */
+ free(indices);
+}
+
+static int
+handle_get_bridges_cmd(const struct ovsrec_open_vswitch *ovs,
+ struct ofpbuf *buffer)
+{
+ struct svec bridges;
+ size_t i, j;
+
+ uint32_t seq;
+
+ int error;
+
+ /* Parse Netlink command.
+ *
+ * The command doesn't actually have any arguments, but we need the
+ * sequence number to send the reply. */
+ error = parse_command(buffer, &seq, NULL, NULL, NULL, NULL);
+ if (error) {
+ return error;
+ }
+
+ /* Get all the real bridges and all the fake ones. */
+ svec_init(&bridges);
+ for (i = 0; i < ovs->n_bridges; i++) {
+ const struct ovsrec_bridge *br = ovs->bridges[i];
+
+ svec_add(&bridges, br->name);
+ for (j = 0; j < br->n_ports; j++) {
+ const struct ovsrec_port *port = br->ports[j];
+
+ if (port->fake_bridge) {
+ svec_add(&bridges, port->name);
+ }
+ }
+ }
+
+ send_ifindex_reply(seq, &bridges);
+ svec_destroy(&bridges);
+
+ return 0;
+}
+