+ facet = facet_create(rule, flow);
+ }
+
+ subfacet = subfacet_create(facet,
+ miss->key_fitness, miss->key, miss->key_len,
+ miss->initial_tci);
+
+ LIST_FOR_EACH_SAFE (packet, next_packet, list_node, &miss->packets) {
+ struct dpif_flow_stats stats;
+ struct flow_miss_op *op;
+ struct dpif_execute *execute;
+
+ ofproto->n_matches++;
+
+ if (facet->rule->up.cr.priority == FAIL_OPEN_PRIORITY) {
+ /*
+ * Extra-special case for fail-open mode.
+ *
+ * We are in fail-open mode and the packet matched the fail-open
+ * rule, but we are connected to a controller too. We should send
+ * the packet up to the controller in the hope that it will try to
+ * set up a flow and thereby allow us to exit fail-open.
+ *
+ * See the top-level comment in fail-open.c for more information.
+ */
+ send_packet_in_miss(ofproto, packet, flow);
+ }
+
+ if (!facet->may_install || !subfacet->actions) {
+ subfacet_make_actions(subfacet, packet);
+ }
+
+ dpif_flow_stats_extract(&facet->flow, packet, &stats);
+ subfacet_update_stats(subfacet, &stats);
+
+ if (!subfacet->actions_len) {
+ /* No actions to execute, so skip talking to the dpif. */
+ continue;
+ }
+
+ if (flow->vlan_tci != subfacet->initial_tci) {
+ /* This packet was received on a VLAN splinter port. We added
+ * a VLAN to the packet to make the packet resemble the flow,
+ * but the actions were composed assuming that the packet
+ * contained no VLAN. So, we must remove the VLAN header from
+ * the packet before trying to execute the actions. */
+ eth_pop_vlan(packet);
+ }
+
+ op = &ops[(*n_ops)++];
+ execute = &op->dpif_op.u.execute;
+ op->subfacet = subfacet;
+ op->dpif_op.type = DPIF_OP_EXECUTE;
+ execute->key = miss->key;
+ execute->key_len = miss->key_len;
+ execute->actions = (facet->may_install
+ ? subfacet->actions
+ : xmemdup(subfacet->actions,
+ subfacet->actions_len));
+ execute->actions_len = subfacet->actions_len;
+ execute->packet = packet;
+ }
+
+ if (facet->may_install && subfacet->key_fitness != ODP_FIT_TOO_LITTLE) {
+ struct flow_miss_op *op = &ops[(*n_ops)++];
+ struct dpif_flow_put *put = &op->dpif_op.u.flow_put;
+
+ op->subfacet = subfacet;
+ op->dpif_op.type = DPIF_OP_FLOW_PUT;
+ put->flags = DPIF_FP_CREATE | DPIF_FP_MODIFY;
+ put->key = miss->key;
+ put->key_len = miss->key_len;
+ put->actions = subfacet->actions;
+ put->actions_len = subfacet->actions_len;
+ put->stats = NULL;
+ }
+}
+
+/* Like odp_flow_key_to_flow(), this function converts the 'key_len' bytes of
+ * OVS_KEY_ATTR_* attributes in 'key' to a flow structure in 'flow' and returns
+ * an ODP_FIT_* value that indicates how well 'key' fits our expectations for
+ * what a flow key should contain.
+ *
+ * This function also includes some logic to help make VLAN splinters
+ * transparent to the rest of the upcall processing logic. In particular, if
+ * the extracted in_port is a VLAN splinter port, it replaces flow->in_port by
+ * the "real" port, sets flow->vlan_tci correctly for the VLAN of the VLAN
+ * splinter port, and pushes a VLAN header onto 'packet' (if it is nonnull).
+ *
+ * Sets '*initial_tci' to the VLAN TCI with which the packet was really
+ * received, that is, the actual VLAN TCI extracted by odp_flow_key_to_flow().
+ * (This differs from the value returned in flow->vlan_tci only for packets
+ * received on VLAN splinters.)
+ */
+static enum odp_key_fitness
+ofproto_dpif_extract_flow_key(const struct ofproto_dpif *ofproto,
+ const struct nlattr *key, size_t key_len,
+ struct flow *flow, ovs_be16 *initial_tci,
+ struct ofpbuf *packet)
+{
+ enum odp_key_fitness fitness;
+ uint16_t realdev;
+ int vid;
+
+ fitness = odp_flow_key_to_flow(key, key_len, flow);
+ if (fitness == ODP_FIT_ERROR) {
+ return fitness;
+ }
+ *initial_tci = flow->vlan_tci;
+
+ realdev = vsp_vlandev_to_realdev(ofproto, flow->in_port, &vid);
+ if (realdev) {
+ /* Cause the flow to be processed as if it came in on the real device
+ * with the VLAN device's VLAN ID. */
+ flow->in_port = realdev;
+ flow->vlan_tci = htons((vid & VLAN_VID_MASK) | VLAN_CFI);
+ if (packet) {
+ /* Make the packet resemble the flow, so that it gets sent to an
+ * OpenFlow controller properly, so that it looks correct for
+ * sFlow, and so that flow_extract() will get the correct vlan_tci
+ * if it is called on 'packet'.
+ *
+ * The allocated space inside 'packet' probably also contains
+ * 'key', that is, both 'packet' and 'key' are probably part of a
+ * struct dpif_upcall (see the large comment on that structure
+ * definition), so pushing data on 'packet' is in general not a
+ * good idea since it could overwrite 'key' or free it as a side
+ * effect. However, it's OK in this special case because we know
+ * that 'packet' is inside a Netlink attribute: pushing 4 bytes
+ * will just overwrite the 4-byte "struct nlattr", which is fine
+ * since we don't need that header anymore. */
+ eth_push_vlan(packet, flow->vlan_tci);
+ }
+
+ /* Let the caller know that we can't reproduce 'key' from 'flow'. */
+ if (fitness == ODP_FIT_PERFECT) {
+ fitness = ODP_FIT_TOO_MUCH;
+ }
+ }
+
+ return fitness;
+}
+
+static void
+handle_miss_upcalls(struct ofproto_dpif *ofproto, struct dpif_upcall *upcalls,
+ size_t n_upcalls)
+{
+ struct dpif_upcall *upcall;
+ struct flow_miss *miss, *next_miss;
+ struct flow_miss_op flow_miss_ops[FLOW_MISS_MAX_BATCH * 2];
+ struct dpif_op *dpif_ops[FLOW_MISS_MAX_BATCH * 2];
+ struct hmap todo;
+ size_t n_ops;
+ size_t i;
+
+ if (!n_upcalls) {
+ return;
+ }
+
+ /* Construct the to-do list.
+ *
+ * This just amounts to extracting the flow from each packet and sticking
+ * the packets that have the same flow in the same "flow_miss" structure so
+ * that we can process them together. */
+ hmap_init(&todo);
+ for (upcall = upcalls; upcall < &upcalls[n_upcalls]; upcall++) {
+ enum odp_key_fitness fitness;
+ struct flow_miss *miss;
+ ovs_be16 initial_tci;
+ struct flow flow;
+
+ /* Obtain metadata and check userspace/kernel agreement on flow match,
+ * then set 'flow''s header pointers. */
+ fitness = ofproto_dpif_extract_flow_key(ofproto,
+ upcall->key, upcall->key_len,
+ &flow, &initial_tci,
+ upcall->packet);
+ if (fitness == ODP_FIT_ERROR) {
+ ofpbuf_delete(upcall->packet);
+ continue;
+ }
+ flow_extract(upcall->packet, flow.skb_priority, flow.tun_id,
+ flow.in_port, &flow);
+
+ /* Handle 802.1ag, LACP, and STP specially. */
+ if (process_special(ofproto, &flow, upcall->packet)) {
+ ofproto_update_local_port_stats(&ofproto->up,
+ 0, upcall->packet->size);
+ ofpbuf_delete(upcall->packet);
+ ofproto->n_matches++;
+ continue;
+ }
+
+ /* Add other packets to a to-do list. */
+ miss = flow_miss_create(&todo, &flow, fitness,
+ upcall->key, upcall->key_len, initial_tci);
+ list_push_back(&miss->packets, &upcall->packet->list_node);
+ }
+
+ /* Process each element in the to-do list, constructing the set of
+ * operations to batch. */
+ n_ops = 0;
+ HMAP_FOR_EACH (miss, hmap_node, &todo) {
+ handle_flow_miss(ofproto, miss, flow_miss_ops, &n_ops);