+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;
+
+ struct ofpbuf query_data;
+ char *unixctl_command;
+ uint64_t count, skip;
+ const char *br_name;
+ struct svec ifaces;
+ char *output;
+ char *save_ptr;
+ uint32_t seq;
+ int error;
+
+ /* Parse the command received from brcompat_mod. */
+ error = parse_command(buffer, &seq, &br_name, NULL, &count, &skip);
+ if (error) {
+ return error;
+ }
+
+ /* Fetch the forwarding database using ovs-appctl. */
+ unixctl_command = xasprintf("fdb/show %s", br_name);
+ error = execute_appctl_command(unixctl_command, &output);
+ free(unixctl_command);
+ if (error) {
+ 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. */
+ cfg_read();
+ get_bridge_ifaces(br_name, &ifaces);
+ 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];
+ if (!netdev_nodev_get_etheraddr(iface_name, mac->addr)) {
+ n_local_macs++;
+ }
+ }
+ 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 (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);
+
+ return 0;
+}
+