+/* Returns the name of the bridge that contains a port named 'port_name', as a
+ * malloc'd string that the caller must free, or a null pointer if no bridge
+ * contains a port named 'port_name'. */
+static char *
+get_bridge_containing_port(const char *port_name)
+{
+ struct svec matches;
+ const char *start, *end;
+
+ svec_init(&matches);
+ cfg_get_matches(&matches, "bridge.*.port=%s", port_name);
+ if (!matches.n) {
+ return 0;
+ }
+
+ start = matches.names[0] + strlen("bridge.");
+ end = strstr(start, ".port=");
+ assert(end);
+ return xmemdup0(start, end - start);
+}
+
+static int
+handle_fdb_query_cmd(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_bridge; /* Name used by brctl. */
+ char *ovs_bridge; /* Name used by ovs-vswitchd. */
+ int br_vlan; /* VLAN tag. */
+ struct svec ifaces;
+
+ struct ofpbuf query_data;
+ 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_bridge, NULL, &count, &skip);
+ if (error) {
+ return error;
+ }
+
+ /* Figure out vswitchd bridge and VLAN. */
+ cfg_read();
+ if (bridge_exists(linux_bridge)) {
+ /* Bridge name is the same. We are interested in VLAN 0. */
+ ovs_bridge = xstrdup(linux_bridge);
+ br_vlan = 0;
+ } else {
+ /* No such Open vSwitch bridge 'linux_bridge', but there might be an
+ * internal port named 'linux_bridge' on some other bridge
+ * 'ovs_bridge'. If so then we are interested in the VLAN assigned to
+ * port 'linux_bridge' on the bridge named 'ovs_bridge'. */
+ const char *port_name = linux_bridge;
+
+ ovs_bridge = get_bridge_containing_port(port_name);
+ br_vlan = cfg_get_vlan(0, "vlan.%s.tag", port_name);
+ if (!ovs_bridge || br_vlan < 0) {
+ free(ovs_bridge);
+ send_reply(seq, ENODEV, NULL);
+ return error;
+ }
+ }
+
+ /* Fetch the forwarding database using ovs-appctl. */
+ unixctl_command = xasprintf("fdb/show %s", ovs_bridge);
+ error = execute_appctl_command(unixctl_command, &output);
+ free(unixctl_command);
+ if (error) {
+ free(ovs_bridge);
+ send_reply(seq, error, NULL);
+ 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. */
+ 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(iface_name, NETDEV_ETH_TYPE_NONE, &netdev);
+ if (netdev) {
+ 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) {
+ 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);
+
+ send_reply(seq, 0, &query_data);
+ ofpbuf_uninit(&query_data);
+ free(ovs_bridge);
+
+ return 0;
+}
+