+struct ovn_port {
+ struct hmap_node key_node; /* Index on 'key'. */
+ char *key; /* nbs->name, nbr->name, sb->logical_port. */
+ char *json_key; /* 'key', quoted for use in JSON. */
+
+ const struct nbrec_logical_port *nbs; /* May be NULL. */
+ const struct nbrec_logical_router_port *nbr; /* May be NULL. */
+ const struct sbrec_port_binding *sb; /* May be NULL. */
+
+ /* Logical router port data. */
+ ovs_be32 ip, mask; /* 192.168.10.123/24. */
+ ovs_be32 network; /* 192.168.10.0. */
+ ovs_be32 bcast; /* 192.168.10.255. */
+ struct eth_addr mac;
+ struct ovn_port *peer;
+
+ struct ovn_datapath *od;
+
+ struct ovs_list list; /* In list of similar records. */
+};
+
+static struct ovn_port *
+ovn_port_create(struct hmap *ports, const char *key,
+ const struct nbrec_logical_port *nbs,
+ const struct nbrec_logical_router_port *nbr,
+ const struct sbrec_port_binding *sb)
+{
+ struct ovn_port *op = xzalloc(sizeof *op);
+
+ struct ds json_key = DS_EMPTY_INITIALIZER;
+ json_string_escape(key, &json_key);
+ op->json_key = ds_steal_cstr(&json_key);
+
+ op->key = xstrdup(key);
+ op->sb = sb;
+ op->nbs = nbs;
+ op->nbr = nbr;
+ hmap_insert(ports, &op->key_node, hash_string(op->key, 0));
+ return op;
+}
+
+static void
+ovn_port_destroy(struct hmap *ports, struct ovn_port *port)
+{
+ if (port) {
+ /* Don't remove port->list. It is used within build_ports() as a
+ * private list and once we've exited that function it is not safe to
+ * use it. */
+ hmap_remove(ports, &port->key_node);
+ free(port->json_key);
+ free(port->key);
+ free(port);
+ }
+}
+
+static struct ovn_port *
+ovn_port_find(struct hmap *ports, const char *name)
+{
+ struct ovn_port *op;
+
+ HMAP_FOR_EACH_WITH_HASH (op, key_node, hash_string(name, 0), ports) {
+ if (!strcmp(op->key, name)) {
+ return op;
+ }
+ }
+ return NULL;
+}
+
+static uint32_t
+ovn_port_allocate_key(struct ovn_datapath *od)
+{
+ return allocate_tnlid(&od->port_tnlids, "port",
+ (1u << 15) - 1, &od->port_key_hint);
+}
+
+static void
+join_logical_ports(struct northd_context *ctx,
+ struct hmap *datapaths, struct hmap *ports,
+ struct ovs_list *sb_only, struct ovs_list *nb_only,
+ struct ovs_list *both)
+{
+ hmap_init(ports);
+ list_init(sb_only);
+ list_init(nb_only);
+ list_init(both);
+
+ const struct sbrec_port_binding *sb;
+ SBREC_PORT_BINDING_FOR_EACH (sb, ctx->ovnsb_idl) {
+ struct ovn_port *op = ovn_port_create(ports, sb->logical_port,
+ NULL, NULL, sb);
+ list_push_back(sb_only, &op->list);
+ }
+
+ struct ovn_datapath *od;
+ HMAP_FOR_EACH (od, key_node, datapaths) {
+ if (od->nbs) {
+ for (size_t i = 0; i < od->nbs->n_ports; i++) {
+ const struct nbrec_logical_port *nbs = od->nbs->ports[i];
+ struct ovn_port *op = ovn_port_find(ports, nbs->name);
+ if (op) {
+ if (op->nbs || op->nbr) {
+ static struct vlog_rate_limit rl
+ = VLOG_RATE_LIMIT_INIT(5, 1);
+ VLOG_WARN_RL(&rl, "duplicate logical port %s",
+ nbs->name);
+ continue;
+ }
+ op->nbs = nbs;
+ list_remove(&op->list);
+ list_push_back(both, &op->list);
+ } else {
+ op = ovn_port_create(ports, nbs->name, nbs, NULL, NULL);
+ list_push_back(nb_only, &op->list);
+ }
+
+ op->od = od;
+ }
+ } else {
+ for (size_t i = 0; i < od->nbr->n_ports; i++) {
+ const struct nbrec_logical_router_port *nbr
+ = od->nbr->ports[i];
+
+ struct eth_addr mac;
+ if (!eth_addr_from_string(nbr->mac, &mac)) {
+ static struct vlog_rate_limit rl
+ = VLOG_RATE_LIMIT_INIT(5, 1);
+ VLOG_WARN_RL(&rl, "bad 'mac' %s", nbr->mac);
+ continue;
+ }
+
+ ovs_be32 ip, mask;
+ char *error = ip_parse_masked(nbr->network, &ip, &mask);
+ if (error || mask == OVS_BE32_MAX || !ip_is_cidr(mask)) {
+ static struct vlog_rate_limit rl
+ = VLOG_RATE_LIMIT_INIT(5, 1);
+ VLOG_WARN_RL(&rl, "bad 'network' %s", nbr->network);
+ free(error);
+ continue;
+ }
+
+ struct ovn_port *op = ovn_port_find(ports, nbr->name);
+ if (op) {
+ if (op->nbs || op->nbr) {
+ static struct vlog_rate_limit rl
+ = VLOG_RATE_LIMIT_INIT(5, 1);
+ VLOG_WARN_RL(&rl, "duplicate logical router port %s",
+ nbr->name);
+ continue;
+ }
+ op->nbr = nbr;
+ list_remove(&op->list);
+ list_push_back(both, &op->list);
+ } else {
+ op = ovn_port_create(ports, nbr->name, NULL, nbr, NULL);
+ list_push_back(nb_only, &op->list);
+ }
+
+ op->ip = ip;
+ op->mask = mask;
+ op->network = ip & mask;
+ op->bcast = ip | ~mask;
+ op->mac = mac;
+
+ op->od = od;
+ }
+ }
+ }
+
+ /* Connect logical router ports, and logical switch ports of type "router",
+ * to their peers. */
+ struct ovn_port *op;
+ HMAP_FOR_EACH (op, key_node, ports) {
+ if (op->nbs && !strcmp(op->nbs->type, "router")) {
+ const char *peer_name = smap_get(&op->nbs->options, "router-port");
+ if (!peer_name) {
+ continue;
+ }
+
+ struct ovn_port *peer = ovn_port_find(ports, peer_name);
+ if (!peer || !peer->nbr) {
+ continue;
+ }
+
+ peer->peer = op;
+ op->peer = peer;
+ op->od->router_ports = xrealloc(
+ op->od->router_ports,
+ sizeof *op->od->router_ports * (op->od->n_router_ports + 1));
+ op->od->router_ports[op->od->n_router_ports++] = op;
+ } else if (op->nbr && op->nbr->peer) {
+ op->peer = ovn_port_find(ports, op->nbr->name);
+ }
+ }
+}
+
+static void
+ovn_port_update_sbrec(const struct ovn_port *op)
+{
+ sbrec_port_binding_set_datapath(op->sb, op->od->sb);
+ if (op->nbr) {
+ sbrec_port_binding_set_type(op->sb, "patch");
+
+ const char *peer = op->peer ? op->peer->key : "<error>";
+ const struct smap ids = SMAP_CONST1(&ids, "peer", peer);
+ sbrec_port_binding_set_options(op->sb, &ids);
+
+ sbrec_port_binding_set_parent_port(op->sb, NULL);
+ sbrec_port_binding_set_tag(op->sb, NULL, 0);
+ sbrec_port_binding_set_mac(op->sb, NULL, 0);
+ } else {
+ if (strcmp(op->nbs->type, "router")) {
+ sbrec_port_binding_set_type(op->sb, op->nbs->type);
+ sbrec_port_binding_set_options(op->sb, &op->nbs->options);
+ } else {
+ sbrec_port_binding_set_type(op->sb, "patch");
+
+ const char *router_port = smap_get(&op->nbs->options,
+ "router-port");
+ if (!router_port) {
+ router_port = "<error>";
+ }
+ const struct smap ids = SMAP_CONST1(&ids, "peer", router_port);
+ sbrec_port_binding_set_options(op->sb, &ids);
+ }
+ sbrec_port_binding_set_parent_port(op->sb, op->nbs->parent_name);
+ sbrec_port_binding_set_tag(op->sb, op->nbs->tag, op->nbs->n_tag);
+ sbrec_port_binding_set_mac(op->sb, (const char **) op->nbs->addresses,
+ op->nbs->n_addresses);
+ }
+}
+
+static void
+build_ports(struct northd_context *ctx, struct hmap *datapaths,
+ struct hmap *ports)
+{
+ struct ovs_list sb_only, nb_only, both;
+
+ join_logical_ports(ctx, datapaths, ports, &sb_only, &nb_only, &both);
+
+ /* For logical ports that are in both databases, update the southbound
+ * record based on northbound data. Also index the in-use tunnel_keys. */
+ struct ovn_port *op, *next;
+ LIST_FOR_EACH_SAFE (op, next, list, &both) {
+ ovn_port_update_sbrec(op);
+
+ add_tnlid(&op->od->port_tnlids, op->sb->tunnel_key);
+ if (op->sb->tunnel_key > op->od->port_key_hint) {
+ op->od->port_key_hint = op->sb->tunnel_key;
+ }
+ }
+
+ /* Add southbound record for each unmatched northbound record. */
+ LIST_FOR_EACH_SAFE (op, next, list, &nb_only) {
+ uint16_t tunnel_key = ovn_port_allocate_key(op->od);
+ if (!tunnel_key) {
+ continue;
+ }
+
+ op->sb = sbrec_port_binding_insert(ctx->ovnsb_txn);
+ ovn_port_update_sbrec(op);
+
+ sbrec_port_binding_set_logical_port(op->sb, op->key);
+ sbrec_port_binding_set_tunnel_key(op->sb, tunnel_key);
+ }
+
+ /* Delete southbound records without northbound matches. */
+ LIST_FOR_EACH_SAFE(op, next, list, &sb_only) {
+ list_remove(&op->list);
+ sbrec_port_binding_delete(op->sb);
+ ovn_port_destroy(ports, op);
+ }
+}
+\f
+#define OVN_MIN_MULTICAST 32768
+#define OVN_MAX_MULTICAST 65535
+
+struct multicast_group {
+ const char *name;
+ uint16_t key; /* OVN_MIN_MULTICAST...OVN_MAX_MULTICAST. */
+};
+
+#define MC_FLOOD "_MC_flood"
+static const struct multicast_group mc_flood = { MC_FLOOD, 65535 };
+
+#define MC_UNKNOWN "_MC_unknown"
+static const struct multicast_group mc_unknown = { MC_UNKNOWN, 65534 };
+
+static bool
+multicast_group_equal(const struct multicast_group *a,
+ const struct multicast_group *b)
+{
+ return !strcmp(a->name, b->name) && a->key == b->key;
+}
+
+/* Multicast group entry. */
+struct ovn_multicast {
+ struct hmap_node hmap_node; /* Index on 'datapath' and 'key'. */
+ struct ovn_datapath *datapath;
+ const struct multicast_group *group;
+
+ struct ovn_port **ports;
+ size_t n_ports, allocated_ports;
+};
+
+static uint32_t
+ovn_multicast_hash(const struct ovn_datapath *datapath,
+ const struct multicast_group *group)
+{
+ return hash_pointer(datapath, group->key);
+}
+
+static struct ovn_multicast *
+ovn_multicast_find(struct hmap *mcgroups, struct ovn_datapath *datapath,
+ const struct multicast_group *group)
+{
+ struct ovn_multicast *mc;
+
+ HMAP_FOR_EACH_WITH_HASH (mc, hmap_node,
+ ovn_multicast_hash(datapath, group), mcgroups) {
+ if (mc->datapath == datapath
+ && multicast_group_equal(mc->group, group)) {
+ return mc;
+ }
+ }
+ return NULL;
+}
+
+static void
+ovn_multicast_add(struct hmap *mcgroups, const struct multicast_group *group,
+ struct ovn_port *port)
+{
+ struct ovn_datapath *od = port->od;
+ struct ovn_multicast *mc = ovn_multicast_find(mcgroups, od, group);
+ if (!mc) {
+ mc = xmalloc(sizeof *mc);
+ hmap_insert(mcgroups, &mc->hmap_node, ovn_multicast_hash(od, group));
+ mc->datapath = od;
+ mc->group = group;
+ mc->n_ports = 0;
+ mc->allocated_ports = 4;
+ mc->ports = xmalloc(mc->allocated_ports * sizeof *mc->ports);
+ }
+ if (mc->n_ports >= mc->allocated_ports) {
+ mc->ports = x2nrealloc(mc->ports, &mc->allocated_ports,
+ sizeof *mc->ports);
+ }
+ mc->ports[mc->n_ports++] = port;
+}
+
+static void
+ovn_multicast_destroy(struct hmap *mcgroups, struct ovn_multicast *mc)
+{
+ if (mc) {
+ hmap_remove(mcgroups, &mc->hmap_node);
+ free(mc->ports);
+ free(mc);
+ }
+}
+
+static void
+ovn_multicast_update_sbrec(const struct ovn_multicast *mc,
+ const struct sbrec_multicast_group *sb)
+{
+ struct sbrec_port_binding **ports = xmalloc(mc->n_ports * sizeof *ports);
+ for (size_t i = 0; i < mc->n_ports; i++) {
+ ports[i] = CONST_CAST(struct sbrec_port_binding *, mc->ports[i]->sb);
+ }
+ sbrec_multicast_group_set_ports(sb, ports, mc->n_ports);
+ free(ports);
+}
+\f
+/* Logical flow generation.